import json, re

with open('index_main.html','r',encoding='utf-8') as f:
    html = f.read()

# 提取 CATD 数组
catd_start = html.find('const CATD=')
if catd_start == -1:
    catd_start = html.find('const CATD =')
print(f'CATD 起始位置: {catd_start}')

# 找 CATD = [
arr_start = html.find('[', catd_start)
# 括号深度匹配找结束
depth = 0
i = arr_start
while i < len(html):
    if html[i] == '[':
        depth += 1
    elif html[i] == ']':
        depth -= 1
        if depth == 0:
            arr_end = i
            break
    i += 1

catd_str = html[arr_start:arr_end+1]
# 用 json 解析
try:
    catd = json.loads(catd_str)
    print(f'CATD 条目数: {len(catd)}')
except:
    print('CATD JSON 解析失败，尝试正则')
    # 找包含 ucsd 或 ucla 的条目
    entries = re.findall(r'\{[^{}]*"ucla"[^{}]*\}', catd_str)
    print(f'UCLA 条目数: {len(entries)}')

# 直接在 CATD 字符串里搜关键词
keywords = [
    'Computational Science, Mathematics',
    'Computational Social Science',
    'Advanced Manufacturing',
    'Data Science and Engineering',
    'Geospatial Information',
]

for kw in keywords:
    idx = catd_str.lower().find(kw.lower())
    if idx >= 0:
        # 提取周围 500 字符
        snippet = catd_str[max(0,idx-100):idx+400]
        print(f'\n=== 找到: {kw} ===')
        print(snippet)
    else:
        print(f'\n=== 未找到: {kw} ===')
