# -*- coding: utf-8 -*-
import io, sys, ssl, urllib.request, re, html as _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
slugs = [
    "health-behavior",
    "maternal-child-health",
    "health-policy-management",  # 已有 MSPH，看 MHA/MPH
    "nutrition",  # 已有 MS，看 MPH
    "accounting",  # Kenan-Flagler MAcc
    "business-analytics",
    "management",
    "marketing",
    "finance",
]
for s in slugs:
    # 先试 catalog 路径
    url = f"https://catalog.unc.edu/graduate/schools-departments/{s}/"
    try:
        req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
        r = urllib.request.urlopen(req, timeout=15, context=ctx)
        b = r.read()
        h = b.decode("utf-8", errors="ignore")
        text = re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
        text = re.sub(r"<style[\s\S]*?</style>"," ",text,flags=re.I)
        text = re.sub(r"<[^>]+>"," ",text)
        text = _html.unescape(text); text = re.sub(r"\s+"," ",text)
        i = text.lower().find("master")
        seg = text[max(0,i-80):i+900] if i>=0 else "(no master keyword)"
        excl = "terminal" in text.lower() or "does not offer" in text.lower()
        print(f"=== {s} | {len(b)}B | excl={excl}")
        print(seg[:900])
        print()
    except Exception as e:
        print(f"=== {s} | ERR {e}")
