# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, re
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
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36"
urls = [
  "https://catalog.upenn.edu/graduate/programs/biomedical-engineering/",
  "https://catalog.upenn.edu/graduate/programs/systems-engineerings/",
  "https://catalog.upenn.edu/graduate/programs/robotics/",
  "https://www.sgs.utoronto.ca/programs/information/",
  "https://www.sgs.utoronto.ca/programs/biotechnology/",
  "https://www.sgs.utoronto.ca/programs/biomedical-communication/",
  "https://www.sgs.utoronto.ca/programs/industrial-relations-human-resources/",
  "https://bulletins.nyu.edu/graduate/professional-studies/programs/management-analytics-ms/",
]
for u in urls:
    try:
        req = urllib.request.Request(u, headers={"User-Agent":UA, "Accept-Language":"en"})
        r = urllib.request.urlopen(req, timeout=12, context=SCTX)
        body = r.read()
        text = body.decode("utf-8","ignore")
        text = re.sub(r"<script[\s\S]*?</script>","",text)
        text = re.sub(r"<style[\s\S]*?</style>","",text)
        text = re.sub(r"<[^>]+>"," ",text)
        text = re.sub(r"\s+"," ",text)
        m = re.search(r"(Overview|Program Overview|Quick Facts)([\s\S]{0,900})", text)
        print("="*60)
        print(r.status, len(body), u)
        if m: print(m.group(0)[:900])
    except Exception as e:
        print("ERR", type(e).__name__, str(e)[:80], u)
