# -*- 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)

# UNC geography
url = "https://catalog.unc.edu/graduate/schools-departments/geography/"
st, body = get(url)
print(f"=== geography: {st} {len(body)} ===")
if st == 200:
    # 找 Master 段
    idx = body.find("Master")
    if idx > 0:
        txt = re.sub(r'<[^>]+>', ' ', body[idx:idx+2000])
        txt = h.unescape(re.sub(r'\s+', ' ', txt))
        print(txt[:800])

# UNC health-policy-management（MHA 之前 404，换试 health-policy-management slug）
url2 = "https://catalog.unc.edu/graduate/schools-departments/health-policy-management/"
st2, body2 = get(url2)
print(f"\n=== health-policy-management: {st2} {len(body2)} ===")
if st2 == 200:
    # 找 MHA / MPH 段
    for kw in ["Master of Health Administration", "MHA", "Master of Public Health", "MPH"]:
        idx = body2.find(kw)
        if idx > 0:
            txt = re.sub(r'<[^>]+>', ' ', body2[idx:idx+1000])
            txt = h.unescape(re.sub(r'\s+', ' ', txt))
            print(f"[{kw}]:", txt[:400])
            break

# UNC global-studies
url3 = "https://catalog.unc.edu/graduate/schools-departments/global-studies/"
st3, body3 = get(url3)
print(f"\n=== global-studies: {st3} {len(body3)} ===")
if st3 == 200:
    idx3 = body3.find("Master")
    if idx3 > 0:
        txt3 = re.sub(r'<[^>]+>', ' ', body3[idx3:idx3+1500])
        txt3 = h.unescape(re.sub(r'\s+', ' ', txt3))
        print(txt3[:600])
