# -*- 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"})
    with urllib.request.urlopen(req,timeout=25,context=ctx) as r:
        return r.status,r.read().decode("utf-8","replace")
def clean(h):
    t=re.sub(r"<script[\s\S]*?</script>","",h)
    t=re.sub(r"<style[\s\S]*?</style>","",t)
    t=re.sub(r"<[^>]+>"," ",t); return re.sub(r"\s+"," ",t)
st,h=get("https://harris.uchicago.edu/academics/programs-degrees/degrees/ms-computational-analysis-and-public-policy-mscapp")
print("=== overview sheet / pdf hrefs ===")
for m in re.findall(r'href="([^"]+)"',h):
    if any(x in m.lower() for x in ["pdf","sheet","overview","fact","curricul","catalog","requirement"]):
        print(m)
t=clean(h)
for kw in ["prerequisite","background","programming","coding","Python","math","credit","units","capstone","internship","work experience","class size","cohort","students"]:
    for m in re.finditer(re.escape(kw),t):
        a=max(0,m.start()-130); b=min(len(t),m.end()+220)
        seg=t[a:b].strip()
        if "Menu" in seg[:25]: continue
        print("\n[MN]",kw,"…",seg[:330]); break
print("\n=== policies degree requirements ===")
try:
    st2,h2=get("https://harris.uchicago.edu/student-life/dean-of-students-office/policies")
    t2=clean(h2)
    i=t2.lower().find("grad-degree-requirements")
    for kw in ["credit","MSCAPP","capstone","internship","quarter","requirement"]:
        m=re.search(re.escape(kw),t2)
        if m:
            a=max(0,m.start()-120); b=min(len(t2),m.end()+220)
            print(" -",kw,"…",t2[a:b].strip()[:300])
except Exception as e:
    print("policies ERR",repr(e))
