# -*- coding: utf-8 -*-
import urllib.request, ssl, io, sys, os, re, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
SCTX = ssl.create_default_context(); SCTX.check_hostname=False; SCTX.verify_mode=ssl.CERT_NONE
UA = {"User-Agent":"Mozilla/5.0"}
targets = [
    ("math", "https://catalog.unc.edu/graduate/schools-departments/mathematics/"),
    ("asian", "https://catalog.unc.edu/graduate/schools-departments/asian-studies/"),
    ("classics", "https://catalog.unc.edu/graduate/schools-departments/classics/"),
    ("phil", "https://catalog.unc.edu/graduate/schools-departments/philosophy/"),
    ("socio", "https://catalog.unc.edu/graduate/schools-departments/sociology/"),
]
data = {}
for key, url in targets:
    req = urllib.request.Request(url, headers=UA)
    r = urllib.request.urlopen(req, timeout=15, context=SCTX)
    body = r.read().decode("utf-8", errors="ignore")
    m = re.search(r'<main[^>]*>([\s\S]*?)</main>', body)
    txt = m.group(1) if m else body
    txt = re.sub(r'<script[\s\S]*?</script>', '', txt)
    txt = re.sub(r'<style[\s\S]*?</style>', '', txt)
    txt = re.sub(r'<[^>]+>', ' ', txt)
    txt = re.sub(r'\s+', ' ', txt).strip()
    # 找 master 相关段
    data[key] = txt[:6000]
    print(f"=== {key} ===")
    print(txt[:2500])
    print()
json.dump(data, open("_round73_data.json","w",encoding="utf-8"), ensure_ascii=False, indent=2)
