import ssl, urllib.request, re, sys, io
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

def fetch(u):
    req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
    return urllib.request.urlopen(req, context=ctx, timeout=25).read().decode("utf-8", "ignore")

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

for u in ["https://economics.uchicago.edu/masters-programs/master-arts-economics",
          "https://economics.uchicago.edu/masters-programs/master-arts-economics/apply"]:
    try:
        h = fetch(u)
        t = text(h)
        print("="*30, u, "len=", len(h))
        print(t[:4500])
        print()
    except Exception as e:
        print("ERR", u, type(e).__name__, str(e)[:120])
