# -*- coding: utf-8 -*-
import urllib.request, ssl, re, sys, io
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
urls = [
 ("UNC_Ling", "https://catalog.unc.edu/graduate/schools-departments/linguistics/"),
 ("UNC_Drama", "https://catalog.unc.edu/graduate/schools-departments/dramatic-art/"),
 ("UNC_Music", "https://catalog.unc.edu/graduate/schools-departments/music/"),
]
for name, url in urls:
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    r = urllib.request.urlopen(req, timeout=15, context=SCTX)
    b = r.read().decode("utf-8", errors="ignore")
    text = re.sub(r"<script[\s\S]*?</script>", " ", b, flags=re.I)
    text = re.sub(r"<style[\s\S]*?</style>", " ", text, flags=re.I)
    text = re.sub(r"<[^>]+>", " ", text)
    text = re.sub(r"\s+", " ", text).strip()
    print(f"=== {name} 正文长度 {len(text)} ===")
    # 找硕士项目描述
    for kw in ["Master of Arts", "Master of Music", "Master of Fine Arts", "M.A.", "M.M.", "M.F.A.", "PhD", "doctoral", "graduate program", "Graduate Program"]:
        idx = text.find(kw)
        if idx > 2000:
            print(f"  [{kw}] @{idx}: ...{text[max(0,idx-100):idx+600]}...")
            print()
