import re, sys, io, urllib.request, ssl
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(url):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    with urllib.request.urlopen(req, timeout=12, context=ctx) as resp:
        return resp.status, resp.read().decode("utf-8","ignore")

def clean(body):
    t=re.sub(r'<script[\s\S]*?</script>','',body); t=re.sub(r'<style[\s\S]*?</style>','',t)
    t=re.sub(r'<[^>]+>',' ',t); return re.sub(r'\s+',' ',t)

# 1. INI GRE 完整上下文
st,body=get("https://www.cmu.edu/ini/admissions/howtoapply.html")
t=clean(body)
i=t.lower().find("gre")
print("=== INI GRE 上下文 ===")
print(t[max(0,i-200):i+300] if i>=0 else "none")
print()

# 2. Duke requirements 找 test score / GRE / GMAT
st,body=get("https://masters.pratt.duke.edu/apply/requirements/")
t=clean(body)
for kw in ["GRE","GMAT","test score","standardized","test"]:
    j=t.lower().find(kw.lower())
    if j>=0:
        print(f"=== Duke req [{kw}] ===")
        print(t[max(0,j-120):j+220]); print()
print()

# 3. Tepper BA 找 admissions/requirements 链接
st,body=get("https://www.cmu.edu/tepper/programs/master-business-analytics")
links=dict.fromkeys(re.findall(r'href="([^"]+)"',body))
for l in links:
    if any(k in l.lower() for k in ["admission","apply","requirement","test","gre"]):
        print("Tepper BA link:", l)
