# -*- 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"}
targets={
 "mech_mme":"https://mech.rice.edu/academics/graduate-programs/masters-mechanical-engineering",
 "bioe_mbe":"https://bioe.rice.edu/academics/masters-programs/mbe-applied-bioengineering",
 "cmor_mcaam":"https://cmor.rice.edu/academics/graduate-programs/masters-programs",
 "epmp_prog":"https://epmp.rice.edu/programs",
 "epmp_adm":"https://epmp.rice.edu/admissions",
}
for k,u in targets.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"_rice11c_{k}.html","wb").write(b)
        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"_rice11c_{k}.txt","w",encoding="utf-8").write(txt)
        print(k,len(b),"B txt",len(txt))
    except Exception as e:
        print(k,"ERR",type(e).__name__,str(e)[:50])
