# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, re, json, os
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
UA = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
def get(url):
    req = urllib.request.Request(url, headers=UA)
    return urllib.request.urlopen(req, timeout=20, context=SCTX).read().decode("utf-8","ignore")

def strip(h):
    h = re.sub(r"<script[\s\S]*?</script>", " ", h, flags=re.I)
    h = re.sub(r"<style[\s\S]*?</style>", " ", h, flags=re.I)
    h = re.sub(r"<[^>]+>", " ", h)
    h = re.sub(r"\s+", " ", h)
    return h.strip()

items = [
  ("nyu-hist",  "https://bulletins.nyu.edu/graduate/arts-science/programs/history-ma/", "NYU GSAS History MA"),
  ("nyu-eas",   "https://bulletins.nyu.edu/graduate/arts-science/programs/east-asian-studies-ma/", "NYU GSAS East Asian Studies MA"),
  ("nyu-jir",   "https://bulletins.nyu.edu/graduate/arts-science/programs/journalism-international-relations-ma/", "NYU GSAS Journalism and International Relations MA"),
  ("unc-hist",  "https://catalog.unc.edu/graduate/schools-departments/history/", "UNC History"),
  ("unc-anth",  "https://catalog.unc.edu/graduate/schools-departments/anthropology/", "UNC Anthropology"),
]
out = {}
for tag, url, label in items:
    html = get(url)
    text = strip(html)
    # 提取 Program Requirements / Curriculum / Admissions 段
    print(f"\n========== {tag} {label} ==========")
    print(f"URL: {url}")
    print(f"len={len(text)}")
    # 找学位相关关键字
    for kw in ["Master of Arts", "MA in", "M.A.", "degree", "minimum", "credit", "thesis", "exam", "course", "admission", "TOEFL", "IELTS", "GRE", "application deadline"]:
        idx = text.lower().find(kw.lower())
        if idx>=0:
            seg = text[max(0,idx-100):idx+300]
            print(f"  [{kw}] ...{seg}...")
    out[tag] = {"url": url, "text_head": text[:3000]}
json.dump(out, open("_round74_data.json","w",encoding="utf-8"), ensure_ascii=False, indent=2)
print("\nSaved _round74_data.json")
