# -*- coding: utf-8 -*-
import io, sys, os, ssl, re, urllib.request, html as H
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
UA={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
base="https://cals.cornell.edu"
pages={
 "req":"/education/admissions/graduate-admissions/professional-masters-degrees/master-of-engineering/meng-degree-requirements",
 "guide":"/education/admissions/graduate-admissions/professional-masters-degrees/master-of-engineering-meng",
}
for k,path in pages.items():
    try:
        req=urllib.request.Request(base+path,headers=UA)
        r=urllib.request.urlopen(req,context=ctx,timeout=20)
        txt=r.read().decode("utf-8","ignore")
        open(f"_cornell_bee_{k}.html","w",encoding="utf-8").write(txt)
        body=re.sub(r"<script.*?</script>","",txt,flags=re.S); body=re.sub(r"<style.*?</style>","",body,flags=re.S)
        text=H.unescape(re.sub(r"\s+"," ",re.sub(r"<[^>]+>"," ",body)))
        open(f"_cornell_bee_{k}.txt","w",encoding="utf-8").write(text)
        print("\n====",k,r.status,"len",len(txt),"正文",len(text))
        for kw in ["credit","Core","elective","project","concentration","5000","4000","S/U","letter grade","semester","thesis","admission","deadline","January","November","GRE","TOEFL","IELTS","tuition","fee","GPA","recommendation","Statement","transcript","prerequisite","Distance","online","part-time"]:
            i=text.lower().find(kw.lower())
            if i>=0: print(f"[{kw}] ...{text[max(0,i-70):i+160]}...")
    except Exception as e:
        print(k,"ERR",type(e).__name__,e)
