import urllib.request
import re

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

# 试几个学校的官网
urls = [
    ('UCLA MQE', 'https://economics.ucla.edu/graduate/programs/mqe/'),
    ('UNC ADS', 'https://sils.unc.edu/academics/graduate/masters-applied-data-science/'),
    ('UVA DS', 'https://datascience.virginia.edu/ms-data-science'),
]

for name, url in urls:
    try:
        req = urllib.request.Request(url, headers=headers)
        with urllib.request.urlopen(req, timeout=10) as resp:
            html = resp.read().decode('utf-8', errors='ignore')
            print(f'=== {name} ===')
            print(f'Status: {resp.status}')
            title = re.search(r'<title>(.*?)</title>', html)
            if title:
                print(f'Title: {title.group(1)[:80]}')
            # 找截止
            ddl = re.findall(r'(deadline|Deadline|due|Due|February|December|January)[^.]{0,80}', html)
            print(f'截止: {ddl[:3]}')
            # 找学费
            tui = re.findall(r'(tuition|Tuition|\$[\d,]+)[^.]{0,60}', html)
            print(f'学费: {tui[:3]}')
            # 找语言
            lang = re.findall(r'(TOEFL|IELTS|English proficiency)[^.]{0,60}', html)
            print(f'语言: {lang[:3]}')
            print()
    except Exception as e:
        print(f'=== {name} ===')
        print(f'Error: {str(e)[:80]}')
        print()
