# -*- 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,5000}',req)
t=strip(m.group(0))
i=t.find("Exemption")
print("豁免段:\n", t[i:i+1200] if i>0 else t[-1200:])
print("="*20)
for u in ["https://gradschool.duke.edu/admissions/application-requirements/english-language-proficiency/",
          "https://gradschool.duke.edu/admissions/how-to-apply/english-language-proficiency/",
          "https://gradschool.duke.edu/academics/programs-degrees/electrical-and-computer-engineering-ms/"]:
    try:
        h=get(u); st=strip(h)
        sc=[w for w in ["100","90","80","110","105","7.0","6.5","120","115","Duolingo","TOEFL","IELTS"] if w in st]
        print("OK",u,"len",len(st),"hits",sc)
        j=st.lower().find("english language")
        if j<0: j=st.lower().find("toefl")
        print(st[j:j+600] if j>0 else st[:300])
    except Exception as e:
        print("ERR",u,repr(e)[:100])
