# -*- coding: utf-8 -*-
import ssl, io, sys, urllib.request, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
BASE = r"D:\work buddy\2026-11-05-14-15-52"
u = "https://econ.duke.edu/masters-programs/degree-programs/ma-econ"
try:
    req = urllib.request.Request(u, headers={"User-Agent":"Mozilla/5.0"})
    r = urllib.request.urlopen(req, timeout=15, context=ctx)
    b = r.read(); h = b.decode("utf-8","ignore")
    open(BASE+r"_mae_degree.html","w",encoding="utf-8").write(h)
    body = re.sub(r"<script.*?</script>", " ", h, flags=re.S)
    body = re.sub(r"<style.*?</style>", " ", body, flags=re.S)
    body = html.unescape(re.sub(r"<[^>]+>", " ", body))
    body = re.sub(r"\s+", " ", body).strip()
    print(f"status {r.status} len={len(b)} body={len(body)}")
    print(body[:2600])
    print("\n=== 关键词 ===")
    for kw in ["STEM","credit","tuition","course","semester","track","concentration","thesis","capstone","GPA","prerequisite","math"]:
        m = re.search(kw, body, re.I)
        if m:
            a=max(0,m.start()-70); b2=min(len(body),m.end()+100)
            print(f"[{kw}] ...{body[a:b2]}...")
except Exception as e:
    print("ERR", type(e).__name__, str(e)[:100])
