# -*- coding: utf-8 -*-
import requests, re, sys, io, pathlib
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52"); CACHE = BASE / "cache"
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36",
     "Accept-Language": "en-US,en;q=0.9"}
def fetch(tag, url):
    p = CACHE / (tag + ".html")
    if p.exists() and p.stat().st_size > 2000:
        return p.read_text(encoding="utf-8", errors="ignore"), "cache"
    r = requests.get(url, headers=H, timeout=25, allow_redirects=True)
    p.write_text(r.text, encoding="utf-8", errors="ignore"); return r.text, str(r.status_code)
def text(h):
    h = re.sub(r"<script.*?</script>|<style.*?</style>", " ", h, flags=re.S)
    h = re.sub(r"<[^>]+>", " ", h); return re.sub(r"\s+", " ", h)

# UPenn ESE masters index
h, st = fetch("r346-upenn-ese-masters", "https://www.ese.upenn.edu/education/masters/")
print("upenn index", st)
for m in re.finditer(r'href="([^"]+)"[^>]*>(.*?)</a>', h, re.S):
    href, nm = m.group(1), re.sub(r"<[^>]+>", "", m.group(2)).strip()
    if re.search(r"(ee|electrical|mse|master)", (href + nm).lower()) and nm:
        print("  ", nm[:60], "->", href)
