# -*- 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"})
html=urllib.request.urlopen(req,timeout=25,context=ctx).read().decode("utf-8","ignore")
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()

def seg(a,b,label):
    i=txt.find(a); j=txt.find(b,i+len(a)) if b else len(txt)
    print("\n=== "+label+" ===")
    print(txt[i:j if i>=0 else 0][:900] if i>=0 else "NOT FOUND: "+a)

seg("Department/School","Application deadlines","学院/方向")
seg("Application deadlines","Application materials","截止")
seg("Application materials","Admission requirements","材料")
seg("Admission requirements","English language proficiency","录取")
seg("English language proficiency","Tuition and fees","语言")
seg("Specialization options","Program overview","专项")
seg("Program highlights","Department/School","项目亮点")
# Co-op 出现处
for m in re.finditer("Co-op",txt):
    i=m.start(); print("\n[Co-op]",txt[max(0,i-80):i+120])
