# -*- 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
u="https://bulletin.stanford.edu/programs/CHEME-MS"
req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
try:
    t=urllib.request.urlopen(req,timeout=20,context=ctx).read().decode("utf-8","ignore")
    print("bytes=",len(t))
    c=html.unescape(re.sub(r"\s+"," ",re.sub(r"<[^>]+>"," ",re.sub(r"<script.*?</script>"," ",t,flags=re.S))))
    i=c.find("Program Overview")
    print("\n### OVERVIEW\n",c[i:i+600])
    j=c.find("Units in the Program")
    print("\n### REQUIREMENTS\n",c[j:j+2200] if j>=0 else "units block not found")
    for kw in ["thesis","GPA","prerequisite","online","part-time","STEM","fellowship","track","concentration","letter grade"]:
        k=c.lower().find(kw.lower())
        if k>=0:
            print("\n###",kw,"@",k,"\n",c[k:k+260])
except Exception as e:
    print("ERR",type(e).__name__,e)
