# -*- coding: utf-8 -*-
import io,sys,urllib.request,ssl,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
for slug in ["MATSC-MS","ENERGY-MS","AA-MS"]:
    url=f"https://bulletin.stanford.edu/programs/{slug}"
    try:
        req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
        h=urllib.request.urlopen(req,context=ctx,timeout=20)
        txt=h.read().decode("utf-8","ignore")
        print("=====",slug,"len=",len(txt),"status=",h.status)
        # 去标签
        t=re.sub(r"<script[\s\S]*?</script>","",txt)
        t=re.sub(r"<style[\s\S]*?</style>","",t)
        t=re.sub(r"<[^>]+>"," ",t)
        t=re.sub(r"\s+"," ",t)
        for kw in ["terminal","coursework","units","GPA","prerequisite","admission","online","part-time","HCP","coterminal","PhD","research","core","degree requirement"]:
            i=t.lower().find(kw.lower())
            if i>=0:
                print(f"  [{kw}] ...{t[max(0,i-90):i+160]}...")
        print()
    except Exception as e:
        print("=====",slug,"ERR",repr(e))
