# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, time, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
SCTX = ssl.create_default_context(); SCTX.check_hostname=False; SCTX.verify_mode=ssl.CERT_NONE
slugs = [
 "applied-positive-psychology-mapp",
 "government-administration-mpa",
 "international-mpa",
 "health-care-innovation-mhci",
 "public-health-mph",
]
out = []
for s in slugs:
    url = f"https://catalog.upenn.edu/graduate/programs/{s}/"
    body = ""
    for attempt in range(3):
        try:
            req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0","Referer":"https://catalog.upenn.edu/graduate/programs/"})
            r = urllib.request.urlopen(req, timeout=25, context=SCTX)
            body = r.read().decode("utf-8","ignore")
            if r.status == 200 and len(body) > 1000:
                break
        except Exception as e:
            time.sleep(2)
    text = re.sub(r"<script[\s\S]*?</script>", " ", body)
    text = re.sub(r"<style[\s\S]*?</style>", " ", text)
    text = re.sub(r"<[^>]+>", " ", text)
    text = re.sub(r"\s+", " ", text).strip()
    out.append(f"\n========== {s} (len={len(text)}) ==========")
    out.append(text[:4000])
open("_extract89b_out.txt","w",encoding="utf-8").write("\n".join(out))
print("written")
