# -*- coding: utf-8 -*-
import io, sys, urllib.request, ssl, re, os
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

def fetch(url):
    req=urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    r=urllib.request.urlopen(req, timeout=15, context=ctx)
    return r.status, r.read().decode("utf-8","ignore")

def clean(body):
    t = re.sub(r"<script[\s\S]*?</script>","",body)
    t = re.sub(r"<style[\s\S]*?</style>","",t)
    t = re.sub(r"<[^>]+>"," ",t)
    return re.sub(r"\s+"," ",t)

cands = [
    ("unc_stat", "https://catalog.unc.edu/graduate/schools-departments/statistics-operations-research/"),
    ("unc_biostat", "https://catalog.unc.edu/graduate/schools-departments/biostatistics/"),
    ("unc_econ", "https://catalog.unc.edu/graduate/schools-departments/economics/"),
    ("unc_math", "https://catalog.unc.edu/graduate/schools-departments/mathematics/"),
    ("unc_bioinfo", "https://catalog.unc.edu/graduate/schools-departments/bioinformatics-computational-biology/"),
]
for name, u in cands:
    try:
        s, body = fetch(u)
        t = clean(body)
        print(f"\n===== {name} {s} {len(body)}B =====")
        # find Master's section
        idx = t.find("Master's Degree")
        if idx < 0: idx = t.find("Master of")
        if idx < 0: idx = t.find("M.S.")
        if idx >= 0:
            print(t[max(0,idx-100):idx+1500])
        else:
            print(t[:1500])
    except Exception as e:
        print(f"\n===== {name} ERROR {e} =====")
