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

slugs=["accounting","architecture","immunology","pharmacology","industrial-relations-and-human-resources"]
for s in slugs:
    url=f"https://www.sgs.utoronto.ca/programs/{s}/"
    req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0"})
    r=urllib.request.urlopen(req,timeout=15,context=ctx)
    b=r.read()
    txt=b.decode("utf-8","ignore")
    open(f"_41_{s}.html","w",encoding="utf-8").write(txt)
    # 找 Quick Facts 表
    m=re.search(r'Quick Facts.{0,200}?(<table[^>]*>.*?</table>)', txt, re.S|re.I)
    print(f"\n========== {s} ({len(b)} bytes) ==========")
    if m:
        table=m.group(1)
        # 提取 tr/td
        rows=re.findall(r'<tr[^>]*>(.*?)</tr>', table, re.S)
        for row in rows:
            tds=re.findall(r'<td[^>]*>(.*?)</td>', row, re.S)
            cells=[re.sub(r'<[^>]+>','',td).strip() for td in tds]
            cells=[htmllib.unescape(c).replace("&nbsp;"," ").strip() for c in cells if c.strip()]
            if cells:
                print("  |", " | ".join(cells))
    else:
        print("  (no Quick Facts table found)")
