# -*- 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"})
    try:
        with urllib.request.urlopen(req,timeout=20,context=ctx) as r:
            data=r.read()
            ct=r.headers.get("Content-Type","")
            return r.status,ct,data
    except Exception as e:
        return None,None,repr(e).encode("utf-8")

for u in ["https://stat.uchicago.edu/","https://stat.uchicago.edu/academics/","https://stat.uchicago.edu/academics/master-program-in-statistics/","https://stat.uchicago.edu/academics/ms-in-statistics/"]:
    st,ct,data=get(u)
    txt=data.decode("utf-8","ignore") if isinstance(data,bytes) else str(data)
    print("====",u,"status=",st,"len=",len(txt))
    if st==200:
        # 找含 master/ms 的链接
        links=re.findall(r'href="([^"]*(?:master|ms-in|graduate)[^"]*)"',txt,re.I)
        for l in dict.fromkeys(links): print("  link:",l)
