# -*- 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()
# 从 ECE graduate 页提取内部链接
base=get("https://ece.duke.edu/graduate")
links=sorted(set(re.findall(r'href="([^"]*(?:master|degree|admiss|tuition|requirement|curric)[^"]*)"',base,re.I)))
for l in links: print("LINK",l)
print("="*20)
for u in ["https://gradschool.duke.edu/admissions/application-requirements",
          "https://gradschool.duke.edu/admissions/faq",
          "https://www.pratt.duke.edu/academics/graduate"]:
    try:
        t=strip(get(u)); print("OK",u,"len",len(t)); print(t[:1500]); print("-"*20)
    except Exception as e:
        print("ERR",u,repr(e)[:120])
