# -*- coding: utf-8 -*-
import io, sys, os, ssl, urllib.request, socket, 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
socket.setdefaulttimeout(15)
sub={"adm":"https://keck.usc.edu/biostatistics-ms-program/admission-requirements/","cat":"https://catalogue.usc.edu/preview_program.php?catoid=18&poid=25587&returnto=7464"}
for k,u in sub.items():
    try:
        req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/126.0"})
        b=urllib.request.urlopen(req,context=ctx).read()
        t=b.decode("utf-8","ignore")
        tt=re.sub(r'<script.*?</script>','',t,flags=re.S|re.I); tt=re.sub(r'<style.*?</style>','',tt,flags=re.S|re.I)
        tt=html.unescape(re.sub(r'<[^>]+>',' ',tt)); tt=re.sub(r'\s+',' ',tt)
        open(f"_usc4_bio-{k}.txt","w",encoding="utf-8").write(tt)
        print(k,"OK",len(b),"B txt",len(tt))
    except Exception as e:
        print(k,"ERR",type(e).__name__,str(e)[:50])
