# -*- coding: utf-8 -*-
import sys, io, re, os, requests
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = r"D:\work buddy\2026-11-05-14-15-52\cache"
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36"}
u = "https://cse.nd.edu/graduate/"
r = requests.get(u, headers=H, timeout=25)
print("nd grad", r.status_code, len(r.text))
open(BASE + r"\r347-nd-grad.html", "w", encoding="utf-8", errors="replace").write(r.text)
links = re.findall(r'<a[^>]+href="([^"]+)"[^>]*>(.*?)</a>', r.text, re.S)
for h, txt in links:
    txt = re.sub(r"<[^>]+>", "", txt).strip()
    if re.search(r"(M\.S|MS |master|Master)", txt) or re.search(r"(master|ms-|masters)", h, re.I):
        print("  ", h[:110], "|", txt[:60])
print("=== uchicago catalog grep")
t = open(BASE + r"\r347-uchicago-catalog.html", encoding="utf-8", errors="replace").read()
for kw in ["STAT", "Statistics", "Physical Sciences", "graduate"]:
    for m in re.finditer(kw, t):
        s = max(0, m.start()-80)
        print(kw, "::", re.sub(r"<[^>]+>", " ", t[s:m.start()+120]).replace("\n", " ")[:180])
        break
for h in re.findall(r'href="([^"]+)"', t)[:40]:
    print("  link", h[:110])
