# -*- coding: utf-8 -*-
import io,sys,ssl,urllib.request,re
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
url="https://uwaterloo.ca/future-graduate-students/programs/by-faculty/engineering/mechanical-and-mechatronics-engineering-master-engineering"
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
try:
    html=urllib.request.urlopen(req,timeout=25,context=ctx).read().decode("utf-8","ignore")
    print("LEN",len(html))
    txt=re.sub(r"<script.*?</script>","",html,flags=re.S)
    txt=re.sub(r"<style.*?</style>","",txt,flags=re.S)
    txt=re.sub(r"<[^>]+>"," ",txt)
    txt=re.sub(r"\s+"," ",txt).strip()
    print("TEXT",len(txt))
    for kw in ["Delivery mode","Program type","Learning option","Duration","Term","Admission","English","TOEFL","IELTS","GRE","deadline","Deadline","tuition","Tuition","supervisor","Supervisor","self-funded","specialization","Specialization","Co-op","recommendation","Statement","resume","Resume","transcript","honours","75%","78%","funding","Funding"]:
        for m in re.finditer(re.escape(kw),txt):
            i=m.start(); print("["+kw+"]",txt[max(0,i-60):i+160]); break
except Exception as e:
    print("ERR",type(e).__name__,e)
