# -*- coding: utf-8 -*-
import io, sys, ssl, urllib.request, re, html as _html
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
url = "https://engineering.rice.edu/academics/graduate-programs/graduate-degree-programs"
req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
r = urllib.request.urlopen(req, timeout=20, context=ctx)
h = r.read().decode("utf-8", errors="ignore")
# extract all links
links = re.findall(r'href="([^"]+)"', h)
# filter graduate program links
prog_links = [l for l in links if "graduate-program" in l.lower() or "master" in l.lower() or "ms-" in l.lower() or "m-" in l.lower()]
print("ALL links containing program/master/ms:")
for l in sorted(set(prog_links)):
    print(" ", l)
print()
# also dump text around "master"
text = re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
text = re.sub(r"<style[\s\S]*?</style>"," ",text,flags=re.I)
text = re.sub(r"<[^>]+>"," ",text)
text = _html.unescape(text); text = re.sub(r"\s+"," ",text)
i = text.lower().find("master")
print("TEXT around master:")
print(text[max(0,i-200):i+3000])
