# -*- coding: utf-8 -*-
import urllib.request, ssl, re, io, sys
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://uwaterloo.ca/future-graduate-students/programs/by-faculty/science/chemistry-master-science-msc"
html=urllib.request.urlopen(urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"}),timeout=25,context=ctx).read().decode("utf-8","ignore")
txt=re.sub(r"<script[\s\S]*?</script>"," ",html); txt=re.sub(r"<style[\s\S]*?</style>"," ",txt); txt=re.sub(r"<[^>]+>"," ",txt)
for a,b in [("&nbsp;"," "),("&amp;","&"),("&#039;","'"),("&quot;",'"')]: txt=txt.replace(a,b)
txt=re.sub(r"\s+"," ",txt)
print("GRE x",txt.count("GRE")," GMAT x",txt.count("GMAT")," Resume x",txt.count("Resume")," CV x",txt.count("CV"))
i=txt.find("Program overview")
if i<0: i=txt.find("joint")
print("\n--- 概述 ---"); print(txt[i:i+1100].strip()[:1200])
for kw in ["analytical","organic","inorganic","physical","materials","environmental","biochem","international","visa","permanent resident","citizen"]:
    j=txt.lower().find(kw.lower())
    if j>=0: print("\n",kw,"…",txt[max(0,j-70):j+150].strip())
