# -*- coding: utf-8 -*-
import io,sys,ssl,urllib.request,re
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
u="https://uwaterloo.ca/future-graduate-students/programs/by-faculty/math/statistics-master-math-mmath"
try:
    req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
    t=urllib.request.urlopen(req,timeout=20,context=ctx).read().decode("utf-8","ignore")
    print("status len",len(t))
    def clean(x):
        x=re.sub(r"<script.*?</script>"," ",x,flags=re.S); x=re.sub(r"<style.*?</style>"," ",x,flags=re.S)
        x=re.sub(r"<[^>]+>"," ",x); x=re.sub(r"&nbsp;"," ",x); x=re.sub(r"&amp;","&",x); x=re.sub(r"&#39;","'",x)
        return re.sub(r"\s+"," ",x)
    c=clean(t)
    i=c.lower().find("program overview")
    print(c[i-20:i+500] if i>0 else c[:500])
    print("\n=== 关键字段 ===")
    for kw in ["Delivery mode","Program type","Length","Registration","Study option","Application deadline","Admission requirements","minimum overall","TOEFL","IELTS","GRE","References","Statement","Resume","Transcript","Tuition","online","remote","full-time","co-op","specialization","research","thesis"]:
        j=c.lower().find(kw.lower())
        if j>0: print("["+kw+"]",c[j-40:j+240]); print()
except Exception as e:
    print("FAIL",type(e).__name__,str(e)[:120])
