# -*- coding: utf-8 -*-
import ssl, urllib.request, re, io, sys
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"})
    return urllib.request.urlopen(req,context=ctx,timeout=25).read().decode("utf-8","ignore")
def strip(h):
    h=re.sub(r"<script[\s\S]*?</script>","",h); h=re.sub(r"<style[\s\S]*?</style>","",h)
    h=re.sub(r"<[^>]+>"," ",h); h=re.sub(r"[ \t]+"," ",h)
    h=re.sub(r"\n\s*\n+","\n",h); return h.strip()

em=strip(get("https://ece.duke.edu/academics/masters/"))
print("="*15,"ECE MAST 全文 len",len(em))
# 定位 MS vs MEng 差异段与学分/论文
for kw in ["Difference","thesis","Thesis","credit","Credit","MS","research","Research","30","semester","duration","1.5","2 year","16 month","12 month"]:
    for mm in re.finditer(re.escape(kw),em):
        i=mm.start(); print("…",em[max(0,i-60):i+150].strip()[:220]); break
print("="*15,"ECE MAST 3000-7000")
print(em[3000:7200])

tu=strip(get("https://masters.pratt.duke.edu/admissions/tuition-financial-aid/"))
print("="*15,"TUITION 找 MS")
for mm in re.finditer(r"Master of Science|M\.S\.|MS ",tu):
    i=mm.start(); print("…",tu[max(0,i-80):i+200].strip()[:300])
