# -*- coding: utf-8 -*-
import ssl, io, sys, urllib.request, re, html
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
BASE = r"D:\work buddy\2026-11-05-14-15-52"
urls = [
    ("mae_inst", "https://econ.duke.edu/masters-programs/prospective-students/application-instructions"),
    ("mae_admstat", "https://gradschool.duke.edu/about/statistics/economics-master-s-admissions-and-enrollment-statistics/"),
    ("mae_career", "https://gradschool.duke.edu/about/statistics/economics-master-s-career-outcomes-statistics/"),
]
def strip(t):
    t = re.sub(r"<script.*?</script>", " ", t, flags=re.S)
    t = re.sub(r"<style.*?</style>", " ", t, flags=re.S)
    t = html.unescape(re.sub(r"<[^>]+>", " ", t))
    return re.sub(r"\s+", " ", t).strip()
for tag, u in urls:
    try:
        req = urllib.request.Request(u, headers={"User-Agent":"Mozilla/5.0"})
        r = urllib.request.urlopen(req, timeout=15, context=ctx)
        b = r.read(); h = b.decode("utf-8","ignore")
        open(BASE+rf"_{tag}.html","w",encoding="utf-8").write(h)
        body = strip(h)
        print(f"=== {tag} {r.status} len={len(b)} body={len(body)} ===")
        print(body[:900]); print()
    except Exception as e:
        print(f"ERR {tag} {type(e).__name__} {str(e)[:80]}")
