"""
探索详情ID来源 - 简化版
"""
import asyncio
import json
from playwright.async_api import async_playwright


async def explore():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        
        # 增加超时
        page.set_default_timeout(60000)
        
        print("访问页面...")
        await page.goto(
            "https://kd.nsfc.cn/finalSearchList?s=人工智能",
            wait_until="domcontentloaded"
        )
        await page.wait_for_timeout(5000)
        
        # 触发搜索
        try:
            await page.click('button:has-text("检索")')
            await page.wait_for_timeout(5000)
        except:
            pass
        
        # 获取 Vue 列表数据
        print("\n查找 Vue 列表数据...")
        list_data = await page.evaluate('''() => {
            const findData = (el) => {
                if (!el) return null;
                if (el.__vue__) {
                    const vm = el.__vue__;
                    if (vm.tableData && Array.isArray(vm.tableData) && vm.tableData.length > 0) {
                        return vm.tableData;
                    }
                    if (vm.$data && vm.$data.tableData) {
                        return vm.$data.tableData;
                    }
                }
                for (const child of (el.children || [])) {
                    const r = findData(child);
                    if (r) return r;
                }
                return null;
            };
            return findData(document.body);
        }''')
        
        if list_data:
            print(f"找到 {len(list_data)} 条数据")
            print(f"\n第一条数据结构:")
            print(json.dumps(list_data[0], ensure_ascii=False, indent=2))
        else:
            print("未找到 tableData")
        
        await browser.close()


if __name__ == "__main__":
    asyncio.run(explore())
