# -*- coding: utf-8 -*-
import io,sys,ssl,urllib.request,re,html
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
def get(u):
    req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
    r=urllib.request.urlopen(req,timeout=20,context=ctx)
    return r.status,r.read().decode("utf-8","ignore")
for slug in ["STATS-MS","BMDS-MS"]:
    u=f"https://bulletin.stanford.edu/programs/{slug}"
    try:
        st,t=get(u)
        c=html.unescape(re.sub(r"\s+"," ",re.sub(r"<[^>]+>"," ",re.sub(r"<script.*?</script>"," ",t,flags=re.S))))
        print(f"\n===== {slug} {st} len={len(t)} =====")
        for kw in ["Program Overview","Requirements Overview","Minimum Units","terminal","GPA","quarter","STEM","GRE","TOEFL","IELTS","tuition","deadline","coterminal","joint","online","part-time","track","specialization","concentration"]:
            i=c.lower().find(kw.lower())
            if i>=0: print(f"[{kw}] {c[max(0,i-40):i+170]}")
    except Exception as e:
        print(slug,"ERR",type(e).__name__,str(e)[:60])
