# -*- 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()

req=get("https://masters.pratt.duke.edu/apply/requirements/")
m=re.search(r'id="english-test"[\s\S]{0,4000}',req)
t=strip(m.group(0))
# 找分数相关
for kw in ["TOEFL","IELTS","Duolingo","score","minimum","100","7.0","120","110","105"]:
    for mm in re.finditer(kw,t):
        i=mm.start(); print("…",t[max(0,i-80):i+120].strip()); break
print("="*20,"english 全文")
print(t[:2200])

print("="*20,"DEGREE 全文找 MS curriculum")
dr=strip(get("https://masters.pratt.duke.edu/programs/degree-requirements/"))
for m in re.finditer(r"Master of Science",dr):
    i=m.start(); seg=dr[i:i+260]
    if "Curriculum" in seg or "credits" in seg or "thesis" in seg.lower() or "research" in seg.lower():
        print("---",seg)
