# -*- coding: utf-8 -*-
import io, sys, os, ssl, urllib.request, urllib.error, re, html as htmllib
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

def fetch(url, timeout=15):
    req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0"})
    r=urllib.request.urlopen(req,timeout=timeout,context=ctx)
    return r.status, r.read()

# 1. NYU Biostatistics MS 详细提取
print("===== NYU Biostatistics MS =====")
txt=open("_43_nyu_biostat.html",encoding="utf-8",errors="ignore").read()
# 提取 Program Requirements / Credits / Admissions 段
for kw in ["Program Requirements","Credits","Admissions","Minimum","GPA","GRE","TOEFL","IELTS","deadline","Tuition","Full-Time"]:
    m=re.search(r'.{0,50}'+re.escape(kw)+r'.{0,300}', txt, re.S|re.I)
    if m:
        t=re.sub(r'<[^>]+>',' ',m.group(0))
        t=htmllib.unescape(re.sub(r'\s+',' ',t)).strip()
        print(f"  [{kw}] {t[:250]}")

# 2. JHU Applied Economics (e-catalogue)
print("\n===== JHU Applied Economics MS =====")
try:
    s,b=fetch("https://e-catalogue.jhu.edu/engineering/graduate/applied-economics-ms/")
    open("_43_jhu_aecon.html","wb").write(b)
    print("status",s,"len",len(b))
    txt=b.decode("utf-8",errors="ignore")
    for kw in ["Credits","Admission","requirement","GPA","GRE","TOEFL","deadline","Tuition","online","on-campus","part-time","full-time"]:
        m=re.search(r'.{0,50}'+re.escape(kw)+r'.{0,250}', txt, re.S|re.I)
        if m:
            t=re.sub(r'<[^>]+>',' ',m.group(0))
            t=htmllib.unescape(re.sub(r'\s+',' ',t)).strip()
            print(f"  [{kw}] {t[:220]}")
except Exception as e:
    print("ERR",e)

# 3. JHU Financial Economics (e-catalogue)
print("\n===== JHU Financial Economics MS =====")
try:
    s,b=fetch("https://e-catalogue.jhu.edu/engineering/graduate/financial-economics-ms/")
    open("_43_jhu_finecon.html","wb").write(b)
    print("status",s,"len",len(b))
except Exception as e:
    print("ERR",e)

# 4. Cornell BEE (CALS)
print("\n===== Cornell BEE =====")
for path in ["https://bee.cals.cornell.edu/graduate-programs","https://bee.cals.cornell.edu/graduate","https://cals.cornell.edu/biological-environmental-engineering/graduate"]:
    try:
        s,b=fetch(path,12)
        print(path,"->",s,len(b))
        if s==200:
            open("_43_cornell_bee.html","wb").write(b)
            break
    except Exception as e:
        print(path,"->",type(e).__name__,str(e)[:60])

# 5. Dartmouth Math MA
print("\n===== Dartmouth Math MA =====")
try:
    s,b=fetch("https://math.dartmouth.edu/graduate-studies/")
    open("_43_dartmath.html","wb").write(b)
    print("status",s,"len",len(b))
    txt=b.decode("utf-8",errors="ignore")
    for kw in ["MA","Master","graduate","PhD","admission","requirement","degree"]:
        m=re.search(r'.{0,50}'+re.escape(kw)+r'.{0,200}', txt, re.S|re.I)
        if m:
            t=re.sub(r'<[^>]+>',' ',m.group(0))
            t=htmllib.unescape(re.sub(r'\s+',' ',t)).strip()
            print(f"  [{kw}] {t[:200]}")
            break
except Exception as e:
    print("ERR",e)
