# -*- coding: utf-8 -*-
import urllib.request, ssl, re, sys, io, os
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
hdr = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}

def fetch(url):
    req = urllib.request.Request(url, headers=hdr)
    r = urllib.request.urlopen(req, timeout=15, context=SCTX)
    return r.read().decode("utf-8", errors="ignore")

# 深挖 UT Adult Education / Higher Education
for key, url in [
    ("ut-adult-edu", "https://www.sgs.utoronto.ca/programs/adult-education/"),
    ("ut-higher-edu", "https://www.sgs.utoronto.ca/programs/higher-education/"),
]:
    html = fetch(url)
    # 提取 Quick Facts 表
    facts = re.findall(r'<th[^>]*>(.*?)</th>\s*<td[^>]*>(.*?)</td>', html, re.S)
    print("===", key, "===")
    for k,v in facts[:15]:
        k2 = re.sub(r'<[^>]+>','',k).strip()
        v2 = re.sub(r'<[^>]+>','',v).strip()
        print(f"  {k2}: {v2[:120]}")
    # 找项目全名
    m = re.search(r'<h1[^>]*>(.*?)</h1>', html, re.S)
    if m: print("  H1:", re.sub(r'<[^>]+>','',m.group(1)).strip())

# 修正路径再探 NYU HPM 和 JHU
print("\n=== retry paths ===")
for key, url in [
    ("nyu-hpm-v2", "https://bulletins.nyu.edu/graduate/public-service/degrees/mpa/health-policy-and-management/"),
    ("nyu-hpm-v3", "https://bulletins.nyu.edu/graduate/public-service/programs/health-policy-management-mpa/"),
    ("jhu-bioethics-v2", "https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/bioethics/"),
    ("jhu-intl-v2", "https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/international-studies/"),
]:
    try:
        h = fetch(url)
        print(key, "200", len(h))
    except Exception as e:
        print(key, "FAIL", str(e)[:60])
