# -*- coding: utf-8 -*-
import io, sys, os, re, ssl, urllib.request, 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
UA={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
P=[
 ("fe","https://viterbigradadmission.usc.edu/programs/masters/msprograms/financial-engineering/ms-financial-engineering/"),
 ("mf","https://dornsife.usc.edu/mathematical-finance/"),
 ("or","https://viterbigradadmission.usc.edu/programs/masters/msprograms/industrial-systems-engineering/ms-operations-research/"),
 ("ise","https://viterbigradadmission.usc.edu/programs/masters/msprograms/industrial-systems-engineering/ms-industrial-systems-engineering/"),
 ("dsl","https://viterbigradadmission.usc.edu/programs/masters/msprograms/data-science/ms-in-data-science-and-law/"),
]
def text(h):
    h=re.sub(r'<script.*?</script>','',h,flags=re.S|re.I)
    h=re.sub(r'<style.*?</style>','',h,flags=re.S|re.I)
    h=re.sub(r'<[^>]+>',' ',h)
    return html.unescape(re.sub(r'\s+',' ',h)).strip()
for key,u in P:
    try:
        req=urllib.request.Request(u,headers=UA)
        r=urllib.request.urlopen(req,timeout=15,context=ctx)
        b=r.read(); t=b.decode("utf-8","ignore")
        open(f"_usc3_{key}.html","w",encoding="utf-8").write(t)
        open(f"_usc3_{key}.txt","w",encoding="utf-8").write(text(t))
        print(key,"OK",r.status,len(b),"B")
    except Exception as e:
        print(key,"ERR",type(e).__name__,str(e)[:60])
