# -*- 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/",
}
slug = "animal-welfare-behavior-msawb"
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")
m = re.search(r"<title>(.*?)</title>", body, re.S)
print(f"status=200 len={len(body)} title={m.group(1).strip() if m else '?'}")
body2 = re.sub(r"<script[\s\S]*?</script>", "", body)
body2 = re.sub(r"<style[\s\S]*?</style>", "", body2)
text = re.sub(r"<[^>]+>", " ", body2)
text = html.unescape(text)
text = re.sub(r"\s+", " ", text).strip()
m2 = re.search(r"(Program of Study|Overview|Curriculum|Degree Requirements)([\s\S]{0,3500})", text)
print(m2.group(0)[:3000] if m2 else text[:3000])
