# -*- coding: utf-8 -*-
import urllib.request, ssl, gzip, io, sys, re, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
hdr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
def fetch(u, t=20):
    try:
        r=urllib.request.urlopen(urllib.request.Request(u,headers=hdr),timeout=t,context=ctx)
        b=r.read()
        if r.headers.get("Content-Encoding")=="gzip": b=gzip.decompress(b)
        return r.status, len(b), b.decode("utf-8","ignore"), r.geturl()
    except Exception as e:
        return -1, 0, str(e), u

# 探 e-catalogue 主站
for u in [
    "https://e-catalogue.jhu.edu/",
    "https://e-catalogue.jhu.edu/engineering/",
    "https://e-catalogue.jhu.edu/engineering/ep/",
]:
    s,n,h,fu = fetch(u)
    print(s, n, fu)
    if s==200:
        # 找项目链接
        links = re.findall(r'href="([^"]*applied-econ[^"]*)"', h, re.I)
        links += re.findall(r'href="([^"]*financial-econ[^"]*)"', h, re.I)
        links += re.findall(r'href="([^"]*bioinformatics[^"]*)"', h, re.I)
        links += re.findall(r'href="([^"]*geographic[^"]*)"', h, re.I)
        links += re.findall(r'href="([^"]*heritage[^"]*)"', h, re.I)
        for l in set(links): print("   ",l)
