# -*- coding: utf-8 -*-
import urllib.request, ssl, gzip, io, sys, re, 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
hdr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
def fetch(u, t=25):
    try:
        r=urllib.request.urlopen(urllib.request.Request(u,headers=hdr),timeout=t,context=ctx)
        b=r.read()
        if r.headers.get("Content-Encoding")=="gzip": b=gzip.decompress(b)
        return r.status, len(b), b.decode("utf-8","ignore")
    except Exception as e:
        return -1, 0, str(e)

def clean(h):
    t=re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
    t=re.sub(r"<style[\s\S]*?</style>"," ",t,flags=re.I)
    t=re.sub(r"<[^>]+>"," ",t)
    return re.sub(r"\s+"," ",t)

# 1. Duke econ masters 深挖
s,n,h = fetch("https://econ.duke.edu/masters/")
print("=== Duke econ/masters ===")
if s==200:
    t=clean(h)
    # 找所有 master 项目名
    for m in re.finditer(r'(Master of Science[^.\n]{5,120}|MS in [A-Z][^.\n]{5,80}|MSEC|Quantitative [^.\n]{5,80})', t):
        print("  ", m.group(0).strip()[:150])
    # 找 QFE
    for kw in ["Quantitative Financial","QFE","MSEC","Economics and Computation","course","credit","GRE","STEM","admission"]:
        m=re.search(r".{0,80}"+re.escape(kw)+r".{0,150}", t, re.I)
        if m: print("  ",kw,"→",m.group(0).strip()[:200])

# 2. UPenn SEAS EE 深挖
s,n,h = fetch("https://www.seas.upenn.edu/graduate/electrical-engineering/")
print("\n=== UPenn SEAS EE ===")
if s==200:
    t=clean(h)
    for kw in ["MSE","Master of Science in Engineering","degree","credit","STEM","GRE","TOEFL","deadline","concentration","specialization","on-campus"]:
        m=re.search(r".{0,80}"+re.escape(kw)+r".{0,150}", t, re.I)
        if m: print("  ",kw,"→",m.group(0).strip()[:200])

# 3. UPenn bigmaster 深挖
s,n,h = fetch("https://www.med.upenn.edu/bigmaster/")
print("\n=== UPenn MSBMI bigmaster ===")
if s==200:
    t=clean(h)
    for kw in ["Master of Science","MSBMI","Biomedical Informatics","credit","STEM","GRE","TOEFL","deadline","on-campus","online","Plan","track","concentration"]:
        m=re.search(r".{0,80}"+re.escape(kw)+r".{0,150}", t, re.I)
        if m: print("  ",kw,"→",m.group(0).strip()[:200])
