# -*- 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
UA = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36"}

def get(url, timeout=20):
    try:
        req = urllib.request.Request(url, headers=UA)
        r = urllib.request.urlopen(req, timeout=timeout, context=ctx)
        return r.status, r.read().decode("utf-8", errors="ignore")
    except Exception as e:
        return -1, str(e)

# 深挖 applied-physical-sciences
url = "https://catalog.unc.edu/graduate/schools-departments/applied-physical-sciences/"
st, body = get(url)
print(f"=== applied-physical-sciences: {st} {len(body)} ===")
# 找所有硕士项目
mas = re.findall(r'(Master[^<]{5,80})', body)
print("masters:", sorted(set(mas))[:20])

# 深挖 applied-professional-studies
url2 = "https://catalog.unc.edu/graduate/schools-departments/applied-professional-studies/"
st2, body2 = get(url2)
print(f"\n=== applied-professional-studies: {st2} {len(body2)} ===")
# 找 MAPS 详细信息
idx = body2.find("master of applied professional studies")
if idx < 0: idx = body2.find("M.A.P.S.")
if idx > 0:
    txt = re.sub(r'<[^>]+>', ' ', body2[idx:idx+2000])
    txt = h.unescape(re.sub(r'\s+', ' ', txt))
    print(txt[:800])

# 深挖 media-journalism MA in Media and Communication
url3 = "https://catalog.unc.edu/graduate/schools-departments/media-journalism/"
st3, body3 = get(url3)
print(f"\n=== media-journalism: {st3} {len(body3)} ===")
idx3 = body3.find("Master of arts in media and communication")
if idx3 > 0:
    txt3 = re.sub(r'<[^>]+>', ' ', body3[idx3:idx3+1500])
    txt3 = h.unescape(re.sub(r'\s+', ' ', txt3))
    print(txt3[:600])
