# -*- 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
UA = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36"}

def get(url, timeout=20):
    try:
        req = urllib.request.Request(url, headers=UA)
        r = urllib.request.urlopen(req, timeout=timeout, context=ctx)
        return r.status, r.read().decode("utf-8", errors="ignore")
    except Exception as e:
        return -1, str(e)

# NYU Wagner MPA PNM / HPM
urls = [
    ("MPA PNM", "https://bulletins.nyu.edu/graduate/schools-public-service/wagner-public-service/public-nonprofit-management-policy-mpa/"),
    ("MPA HPM", "https://bulletins.nyu.edu/graduate/schools-public-service/wagner-public-service/health-policy-management-mpa/"),
]
for name, url in urls:
    st, body = get(url)
    print(f"\n=== {name}: {st} {len(body)} ===")
    if st == 200:
        # 提取 Quick Facts / Degree Requirements
        # 找 credits
        credits = re.findall(r'(\d+)\s*credit', body, re.I)
        print("credits mentions:", credits[:5])
        # 找 program overview
        idx = body.find("Program Overview")
        if idx < 0: idx = body.find("Overview")
        if idx > 0:
            txt = re.sub(r'<[^>]+>', ' ', body[idx:idx+1500])
            txt = h.unescape(re.sub(r'\s+', ' ', txt))
            print("overview:", txt[:600])
        # 找 admission
        idx2 = body.find("Admission")
        if idx2 > 0:
            txt2 = re.sub(r'<[^>]+>', ' ', body[idx2:idx2+800])
            txt2 = h.unescape(re.sub(r'\s+', ' ', txt2))
            print("admission:", txt2[:400])
