# -*- 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
def get(u):
    req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"})
    try:
        with urllib.request.urlopen(req,context=ctx,timeout=20) as r:
            b=r.read(); enc=r.headers.get_content_charset() or "utf-8"
            return r.status,b.decode(enc,errors="replace"),r.geturl()
    except Exception as e:
        return None,"ERR "+repr(e),u

for u in ["https://finmath.uchicago.edu/","http://finmath.uchicago.edu/"]:
    st,body,url=get(u)
    print("===",u,"->",st,url,"len=",len(body))
    if st==200:
        links=re.findall(r'href="([^"]+)"',body)
        keep=[l for l in links if re.search(r'admiss|deadline|tuition|fees|curriculum|degree|requir|apply|english|gre|finance',l,re.I)]
        for l in sorted(set(keep))[:40]: print("  ",l)
        txt=re.sub(r"<script.*?</script>","",body,flags=re.S)
        txt=re.sub(r"<style.*?</style>","",txt,flags=re.S)
        txt=re.sub(r"<[^>]+>"," ",txt); txt=re.sub(r"\s+"," ",txt)
        print("  TXT:",txt[:1200])
        break
