# -*- 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"})
    with urllib.request.urlopen(req,timeout=20,context=ctx) as r:
        return r.read().decode("utf-8","ignore")
def clean(t):
    t=re.sub(r"<script.*?</script>","",t,flags=re.S|re.I)
    t=re.sub(r"<style.*?</style>","",t,flags=re.S|re.I)
    t=re.sub(r"<[^>]+>"," ",t); t=html.unescape(t)
    t=re.sub(r"[ \t]+"," ",t); t=re.sub(r"\n\s*\n+","\n",t)
    return t.strip()

raw=get("https://stat.uchicago.edu/academics/ms-in-statistics/admission-requirements/")
c=clean(raw)
print("==== admission 后半 ====")
print(c[4500:9000])
print("\n==== 学费/费用相关链接 ====")
for l in dict.fromkeys(re.findall(r'href="([^"]*(?:tuition|fee|cost|aid|fellowship|financial)[^"]*)"',raw,re.I)):
    print(" ",l)

for u in ["https://stat.uchicago.edu/academics/ms-in-statistics/statistics-masters-courses/",
          "https://stat.uchicago.edu/academics/ms-in-statistics/careers-in-statistics/"]:
    r2=get(u); print("\n========",u,"len=",len(r2)); print(clean(r2)[:3000])
