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'&nbsp;', ' ', h); h = re.sub(r'&amp;', '&', h); h = re.sub(r'&#39;', "'", h)
    h = re.sub(r'\s+', ' ', h)
    return h.strip()

kw = re.compile(r'(deadline|deadlines|GRE|GMAT|TOEFL|IELTS|Duolingo|language|tuition|fee|credit|credits|curriculum|recommend|statement|essay|writing sample|STEM|OPT|full[- ]time|one[- ]year|two[- ]year|application fee|GPA|prerequisite|mathemat|economics course)', re.I)

for tag, u in [("MAIN","https://economics.uchicago.edu/masters-programs/master-arts-economics"),
               ("APPLY","https://economics.uchicago.edu/masters-programs/master-arts-economics/apply")]:
    h = fetch(u); t = text(h)
    print("="*25, tag, "textlen=", len(t))
    # split into sentences-ish
    chunks = re.split(r'(?<=[.!?])\s+', t)
    seen = set()
    for c in chunks:
        if kw.search(c) and c not in seen and len(c) < 600:
            seen.add(c); print("-", c.strip())
    print()
