# -*- 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"
    try:
        r = requests.get(url, headers=H, timeout=25, allow_redirects=True)
    except Exception as e:
        return "", "ERR " + type(e).__name__
    p.write_text(r.text, encoding="utf-8", errors="ignore"); return r.text, str(r.status_code)

jobs = [
 ("r346-ucsd-math-grad", "https://math.ucsd.edu/students/graduate"),
 ("r346-ucla-degrees", "https://www.anderson.ucla.edu/degrees"),
 ("r346-uchi-academics", "https://stat.uchicago.edu/academics/"),
]
for tag, u in jobs:
    h, st = fetch(tag, u)
    print("==", tag, st, len(h))
    seen = set()
    for m in re.finditer(r'href="([^"]+)"[^>]*>(.*?)</a>', h, re.S):
        href, nm = m.group(1), re.sub(r"<[^>]+>", "", m.group(2)).strip()
        if not nm or len(nm) > 70:
            continue
        key = re.sub(r"<[^>]+>", "", href)
        if key in seen:
            continue
        seen.add(key)
        if re.search(r"(master|graduate|phd|ms|degree|program|course|curricul)", (href + " " + nm).lower()):
            print("   ", nm[:60], "->", href)
