# -*- coding: utf-8 -*-
import io, sys, os, re, ssl, urllib.request, html as h
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def get(url, timeout=20):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    try:
        r = urllib.request.urlopen(req, timeout=timeout, context=ctx)
        return r.status, len(r.read()), r.read().decode("utf-8", errors="replace")
    except Exception as e:
        return None, 0, f"ERR {type(e).__name__}"

# UCSD catalog graduate 项目页通常在 /graduate/programs/ 或 /catalog/grad/
for url in [
    "https://catalog.ucsd.edu/grad.html",
    "https://catalog.ucsd.edu/front/grad.html",
    "https://grad.ucsd.edu/programs/index.html",
    "https://grad.ucsd.edu/academics/degree-programs.html",
    "https://grad.ucsd.edu/programs/",
]:
    st, n, body = get(url)
    print(f"{st} {n:>8}  {url}")
    if st == 200 and n > 5000:
        # 找项目链接
        hrefs = re.findall(r'href="([^"]+)"[^>]*>([^<]{3,80})</a>', body)
        prog = [(h,t.strip()) for h,t in hrefs if re.search(r'/(program|grad|degree|academic)[s]?/', h) and any(k in t.lower() for k in ["ms","m.s","m.a","master","mph","meng","mcs"])]
        for h,t in prog[:30]:
            print("   ", h, "|", t[:80])
