# -*- 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
def get(url, timeout=25):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    r = urllib.request.urlopen(req, timeout=timeout, context=ctx)
    return r.status, r.read().decode("utf-8", errors="replace")

st, body = get("https://catalog.unc.edu/graduate/degree-programs/")
# 找所有 href 指向具体项目的链接（形如 /graduate/degree-programs/xxx/ 或 /academic-programs/xxx/）
hrefs = re.findall(r'href="([^"]+)"', body)
cand = set()
for h in hrefs:
    # UNC catalog 项目页常见形式
    if re.search(r'/(graduate/)?(degree-programs|academic-programs|programs)/[a-z0-9\-]+/?$', h) and "pdf" not in h:
        cand.add(h)
for c in sorted(cand):
    print(c)
print("---total", len(cand))
# 也找 title 里的 program 名字
print("---HTML sample around 'Master'---")
for m in re.finditer(r'.{40}(Master|MS|MA|MPH|MFA|MEng).{40}', body):
    print(" ", m.group(0)[:160])
