# -*- 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__}"

# Brown 主域 academics 路径
for url in [
    "https://www.brown.edu/academics/",
    "https://www.brown.edu/academics/gradschool/",
    "https://www.brown.edu/graduate-programs/",
    "https://graduateschool.brown.edu/academics",
]:
    st, n, body = get(url)
    print(f"{st} {n:>8}  {url}")
    if st == 200 and n > 5000:
        # 找项目链接
        links = re.findall(r'href="([^"]+)"[^>]*>([^<]+)</a>', body)
        prog = [(h,t.strip()) for h,t in links if re.search(r'(master|m\.s|m\.a|phd|ph\.d|sc\.m|master of)', t, re.I) and any(k in t.lower() for k in ["computer","data","econom","biolog","engineer","statistic","math","physic","business","manage","financ","applied","computational","bio","health","public","internation"])]
        for h,t in prog[:20]:
            print("   ", h[:80], "|", t[:80])
        break

# Rice catalog
print("\n--- Rice ---")
for url in [
    "https://ga.rice.edu/graduate-programs/",
    "https://ga.rice.edu/programs/",
    "https://gradients.rice.edu/degree-programs",
    "https://www.rice.edu/academics/graduate",
]:
    st, n, body = get(url)
    print(f"{st} {n:>8}  {url}")
