# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, os, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
SCTX = ssl.create_default_context()
SCTX.check_hostname = False
SCTX.verify_mode = ssl.CERT_NONE
H = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.9",
    "Referer": "https://catalog.upenn.edu/graduate/programs/",
}
slugs = [
    ("genetic-counseling-msgc", "Genetic Counseling MSGC"),
    ("clinical-epidemiology-msce", "Clinical Epidemiology MSCE"),
    ("healthcare-quality-safety-mhqs", "Healthcare Quality and Safety MHQS"),
    ("bioethics-mbe", "Bioethics MBE"),
    ("nutrition-science-msns", "Nutrition Science MSNS"),
]
out = open("_extract85_out.txt", "w", encoding="utf-8")
for slug, name in slugs:
    url = f"https://catalog.upenn.edu/graduate/programs/{slug}/"
    req = urllib.request.Request(url, headers=H)
    body = urllib.request.urlopen(req, timeout=20, context=SCTX).read().decode("utf-8", errors="replace")
    # strip scripts/styles
    body = re.sub(r"<script[\s\S]*?</script>", "", body)
    body = re.sub(r"<style[\s\S]*?</style>", "", body)
    text = re.sub(r"<[^>]+>", " ", body)
    text = html.unescape(text)
    text = re.sub(r"\s+", " ", text).strip()
    # find program description region
    m = re.search(r"(Program of Study|Overview|Admission Requirements|Curriculum|Degree Requirements)([\s\S]{0,4000})", text)
    snippet = m.group(0)[:3500] if m else text[:3500]
    out.write(f"\n\n========== {name} ({slug}) ==========\n")
    out.write(snippet)
out.close()
print("DONE")
