# -*- coding: utf-8 -*-
import io, sys, os, urllib.request, ssl, re, html
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
hdr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36"}
for k,u in {"epmp":"https://epmp.rice.edu/","cmor":"https://cmor.rice.edu/","mech":"https://mech.rice.edu/","bioe":"https://bioe.rice.edu/"}.items():
    try:
        req=urllib.request.Request(u,headers=hdr)
        r=urllib.request.urlopen(req,context=ctx,timeout=20)
        b=r.read(); t=b.decode("utf-8","ignore")
        open(f"_rice11b_{k}.html","wb").write(b)
        links=re.findall(r'href="([^"]*(?:program|graduate|master|admission|degree|ms|mie|mcse|meml|mme|mbe)[^"]*)"',t,re.I)
        links=sorted(set(links))[:30]
        txt=re.sub(r"<script.*?</script>"," ",t,flags=re.S); txt=re.sub(r"<style.*?</style>"," ",txt,flags=re.S)
        txt=html.unescape(re.sub(r"<[^>]+>"," ",txt)); txt=re.sub(r"\s+"," ",txt).strip()
        open(f"_rice11b_{k}.txt","w",encoding="utf-8").write(txt)
        print("="*15,k,len(b),"B txt",len(txt))
        for l in links: print("  ",l)
    except Exception as e:
        print(k,"ERR",type(e).__name__,str(e)[:50])
