# -*- 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={
 "public-policy":"_40_pp.html",
 "information":"_40_info.html",
}
for slug,fn in slugs.items():
    url="https://www.sgs.utoronto.ca/programs/"+slug+"/"
    req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
    r=urllib.request.urlopen(req,timeout=20,context=ctx)
    raw=r.read()
    open(fn,"wb").write(raw)
    txt=raw.decode("utf-8",errors="ignore")
    mt=re.search(r"<h1[^>]*>(.*?)</h1>",txt,re.S)
    title=re.sub(r"<[^>]+>","",mt.group(1)).strip() if mt else "?"
    print("="*70)
    print(slug,"|",title)
    tbls=re.findall(r"<table[^>]*>(.*?)</table>",txt,re.S)
    for ti,t in enumerate(tbls):
        rows=re.findall(r"<tr[^>]*>(.*?)</tr>",t,re.S)
        if len(rows)<3: continue
        print(f"  -- table {ti} ({len(rows)} rows) --")
        for row in rows[:18]:
            cells=re.findall(r"<t[dh][^>]*>(.*?)</t[dh]>",row,re.S)
            cells=[re.sub(r"<[^>]+>","",c).strip() for c in cells]
            cells=[htmllib.unescape(c) for c in cells if c]
            if cells: print("   |", " | ".join(cells))
