# -*- coding: utf-8 -*-
import io, sys, re, ssl, urllib.request, html as H
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")

for fn in ["_cornell_cee_civil-environmental-engineering-meng.html", "_cornell_cee_meng-admission-requirements.html"]:
    b = open(fn, "rb").read().decode("utf-8","ignore")
    links = sorted(set(re.findall(r'href="([^"]+)"', b)))
    print("=====", fn, "=====")
    for l in links:
        if any(k in l.lower() for k in ["degree", "requirement", "credit", "curriculum", "plan", "faq"]):
            print("  link:", l)
    t = re.sub(r"<script.*?</script>", " ", b, flags=re.S)
    t = re.sub(r"<style.*?</style>", " ", t, flags=re.S)
    t = H.unescape(re.sub(r"<[^>]+>", " ", t))
    t = re.sub(r"[ \t]+", " ", t); t = re.sub(r"\n\s*\n+", "\n", t)
    open(fn.replace(".html",".txt"), "w", encoding="utf-8").write(t)
    print("  text bytes:", len(t))

# 探测学位要求候选
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
b = open("_cornell_cee_civil-environmental-engineering-meng.html", "rb").read().decode("utf-8","ignore")
deg_links = [l for l in sorted(set(re.findall(r'href="([^"]+)"', b))) if "/degree/" in l.lower()]
print("\n=== /degree/ 链接 ===")
for l in deg_links:
    print(l)
