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

SCTX = ssl.create_default_context()
SCTX.check_hostname = False
SCTX.verify_mode = ssl.CERT_NONE

candidates = [
    ("Duke MSEC", "https://econ.duke.edu/masters-programs/degree-programs/msec"),
    ("Duke MAPE", "https://econ.duke.edu/masters-programs/degree-programs/mape"),
    ("Duke MAEcon", "https://econ.duke.edu/masters-programs/degree-programs/ma-econ"),
]

for name, url in candidates:
    try:
        req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
        resp = urllib.request.urlopen(req, timeout=15, context=SCTX)
        html = resp.read().decode("utf-8", errors="ignore")
        print(f"\n=== {name} ({resp.status}, {len(html)}B) ===")
        # h1/h2
        heads = re.findall(r'<h[12][^>]*>(.*?)</h[12]>', html, re.S)
        heads = [re.sub(r'<[^>]+>', '', h).strip() for h in heads]
        heads = [h for h in heads if h]
        print("Headers:", heads[:8])
        # 找关键信息：credits, deadline, TOEFL, GRE, tuition
        for kw in ["credit", "deadline", "TOEFL", "IELTS", "GRE", "tuition", "admission", "requirement", "curriculum", "degree", "MS", "MA"]:
            m = re.findall(r'[^.]{0,80}' + kw + r'[^.]{0,120}', html, re.I)
            if m:
                for s in m[:2]:
                    s = re.sub(r'<[^>]+>', ' ', s)
                    s = re.sub(r'\s+', ' ', s).strip()
                    if len(s) > 20:
                        print(f"  [{kw}] {s[:150]}")
    except Exception as e:
        print(f"\n=== {name} ERR: {type(e).__name__}: {e} ===")
