# -*- 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
for poid in ["32940","32611","31861"]:
    url=f"https://catalogue.usc.edu/preview_program.php?catoid=22&poid={poid}&returnto=9453"
    try:
        req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
        r=urllib.request.urlopen(req,timeout=15,context=ctx); b=r.read()
        t=b.decode("utf-8","ignore")
        t=re.sub(r'<script[\s\S]*?</script>',' ',t); t=re.sub(r'<style[\s\S]*?</style>',' ',t)
        t=re.sub(r'<[^>]+>',' ',t); t=html.unescape(t); t=re.sub(r'\s+',' ',t)
        open(f"_usc5_qcb-{poid}.txt","w",encoding="utf-8").write(t)
        print(poid,r.status,len(b),"B text",len(t))
        print("  head:",t[:180])
    except Exception as e:
        print(poid,"ERR",type(e).__name__,str(e)[:60])
