# -*- 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 = {
    "chemistry": "https://catalog.unc.edu/graduate/schools-departments/chemistry/",
    "health-policy-management": "https://catalog.unc.edu/graduate/schools-departments/health-policy-management/",
    "epidemiology": "https://catalog.unc.edu/graduate/schools-departments/epidemiology/",
    "nutrition": "https://catalog.unc.edu/graduate/schools-departments/nutrition/",
    "public-policy": "https://catalog.unc.edu/graduate/schools-departments/public-policy/",
}
for s, url in slugs.items():
    try:
        req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
        r = urllib.request.urlopen(req, timeout=20, context=ctx)
        h = r.read().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)
        # Master's Degree section
        i = text.lower().find("master's degree")
        if i<0: i = text.lower().find("master of")
        seg = text[i:i+2500] if i>=0 else text[:2000]
        print(f"\n========== {s} ==========")
        print(seg[:2500])
    except Exception as e:
        print(f"=== {s} ERR {e}")
