# -*- coding: utf-8 -*-
import urllib.request, ssl, socket, 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
socket.setdefaulttimeout(20)
items = [
    ("ut-astro", "https://sgs.utoronto.ca/programs/astronomy-and-astrophysics/"),
    ("usc-msba", "https://www.marshall.usc.edu/programs/graduate-programs/specialized-masters/ms-business-analytics"),
    ("usc-msf", "https://www.marshall.usc.edu/programs/graduate-programs/specialized-masters/ms-finance"),
    ("usc-stats", "https://dornsife.usc.edu/statsms/"),
    ("usc-apmath", "https://dornsife.usc.edu/mathematics/graduate/"),
]
out = {}
for key, u in items:
    try:
        req = urllib.request.Request(u, headers={"User-Agent":"Mozilla/5.0"})
        r = urllib.request.urlopen(req, context=SCTX, timeout=20)
        body = r.read().decode("utf-8", errors="ignore")
        # 去 tag
        txt = re.sub(r"<script[\s\S]*?</script>", " ", body)
        txt = re.sub(r"<style[\s\S]*?</style>", " ", txt)
        txt = re.sub(r"<[^>]+>", " ", txt)
        txt = re.sub(r"\s+", " ", txt)
        out[key] = {"url":u, "len":len(body), "text":txt[:8000]}
        print(f"=== {key} {r.status} {len(body)} ===")
    except Exception as e:
        print(f"ERR {key} {e}")
json.dump(out, open("_round76_data.json","w",encoding="utf-8"), ensure_ascii=False, indent=2)
