"""精确提取 r280-r285 新增的 30 条 PhD 记录的 url 和详情页官网链接。"""
import re, pathlib

ROOT = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52")
html = (ROOT / "index_main.html").read_text(encoding="utf-8")

# 用括号深度匹配提取 PROGRAMS 数组
m = re.search(r'const\s+PROGRAMS\s*=\s*\[', html)
start = m.end()
depth = 1
i = start
while i < len(html) and depth > 0:
    if html[i] == '[':
        depth += 1
    elif html[i] == ']':
        depth -= 1
    i += 1
prog_seg = html[start:i-1]

# 用括号深度匹配提取每条记录
records = {}
j = 0
while j < len(prog_seg):
    if prog_seg[j] != '{':
        j += 1
        continue
    # 找到匹配的 }
    d = 1
    k = j + 1
    while k < len(prog_seg) and d > 0:
        if prog_seg[k] == '{':
            d += 1
        elif prog_seg[k] == '}':
            d -= 1
        k += 1
    rec = prog_seg[j:k]
    rid = re.search(r'id:"([^"]+)"', rec)
    if rid:
        records[rid.group(1)] = rec
    j = k

target_ids = [
    "usc-marshall-accounting-phd","waterloo-cs-phd","princeton-biophysics-phd","harvard-biostat-phd","mit-be-phd",
    "mit-cee-phd","mit-nse-phd","princeton-eeb-phd","harvard-bmi-phd","waterloo-stat-phd",
    "mit-hasts-phd","mit-idss-ses-phd","princeton-qcb-phd","harvard-hp-phd","ubc-stat-phd",
    "ubc-cs-phd","ubc-ece-phd","ut-ece-phd","mcg-cs-phd","nu-biosci-phd",
    "ut-cs-phd","ubc-me-phd","ubc-civil-phd","ut-stat-phd","yale-stats-phd",
    "mit-polisci-phd","mit-ling-phd","mit-phil-phd","mcg-math-phd","ut-mie-phd",
]

print(f"total records parsed: {len(records)}")
for tid in target_ids:
    if tid in records:
        rec = records[tid]
        s = re.search(r's:"([^"]*)"', rec)
        n = re.search(r'n:"([^"]*)"', rec)
        deg = re.search(r'deg:"([^"]*)"', rec)
        url = re.search(r'url:"([^"]*)"', rec)
        print(f"{tid} | s={s.group(1) if s else '?'} | n={n.group(1) if n else '?'} | deg={deg.group(1) if deg else '?'} | url={url.group(1) if url else '?'}")
    else:
        print(f"{tid} | NOT FOUND")
