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'
}

# 尝试 Berkeley Graduate Bulletin
urls = [
    'https://grad.berkeley.edu/programs/degree-programs/engineering/graduate-programs/electrical-engineering-and-computer-sciences/',
    'https://bulletin.berkeley.edu/colleges-and-schools/engineering/electrical-engineering-computer-sciences/',
]

for url in urls:
    try:
        req = urllib.request.Request(url, headers=headers)
        with urllib.request.urlopen(req, timeout=15) as resp:
            html = resp.read().decode('utf-8', errors='ignore')
            print(f'=== {url} ===')
            print(f'Status: {resp.status}')
            # 提取标题
            title = re.search(r'<title>(.*?)</title>', html)
            if title:
                print(f'Title: {title.group(1)}')
            # 找 MS 相关
            ms = re.findall(r'(Master of Science|M\.S\.|MS in)[^.]{0,100}', html)
            print(f'MS相关: {ms[:5]}')
            # 找学分
            units = re.findall(r'(\d+ units|units required|semester units|total units)[^.]{0,60}', html)
            print(f'学分相关: {units[:5]}')
            print()
    except Exception as e:
        print(f'=== {url} ===')
        print(f'Error: {e}')
        print()
