# -*- coding: utf-8 -*-
import ssl, re, html, io, sys, urllib.request, os
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
H={"User-Agent":"Mozilla/5.0"}
BASE=r"D:\work buddy\2026-11-05-14-15-52"
def fetch(u):
    req=urllib.request.Request(u,headers=H)
    try:
        with urllib.request.urlopen(req,context=ctx,timeout=20) as r:
            b=r.read(); return r.status,b.decode("utf-8","ignore"),len(b)
    except Exception as e:
        return None,repr(e),0

pages={
 "home":"https://publicpolicy.cornell.edu/masters/dspp/",
 "apply":"https://publicpolicy.cornell.edu/masters/dspp/apply/",
 "faq":"https://publicpolicy.cornell.edu/masters/dspp/apply/faq/",
 "curr":"https://publicpolicy.cornell.edu/masters/msdsp/residential/curriculum/",
 "cost":"https://publicpolicy.cornell.edu/masters/msdsp/residential/cost-and-aid/",
}
for k,u in pages.items():
    st,txt,n=fetch(u)
    if st==200:
        open(os.path.join(BASE,f"_dspp_{k}.html"),"w",encoding="utf-8").write(txt)
        body=html.unescape(re.sub(r"<[^>]+>"," ",re.sub(r"<script.*?</script>|<style.*?</style>"," ",txt,flags=re.S)))
        body=re.sub(r"\s+"," ",body)
        print("="*90); print(k,"len",n)
        # 打印含关键词的句子
        for kw in ["application fee","Application Fee","fee waiver","recommendation","Recommendation","Statement of purpose","Resume","resume","video interview","Video interview","transcript","Transcript","Round","deadline","Deadline","capstone","Capstone","course","Course","credits","Credits","track","Track","concentration","career","Career","employ","Employ","placement","Placement","Scholarship","scholarship","aid","Aid","Exempt","exempt"]:
            for m in re.finditer(re.escape(kw),body):
                s=max(0,m.start()-70); e=min(len(body),m.end()+150)
                print(f"  [{kw}] ...{body[s:e]}...")
                break
