"""测试爬虫，分析详情ID获取问题"""
import asyncio
import re
import sys
from pathlib import Path

# 添加项目根目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))

from nsfc_spider.tasks.direct_scraper import NSFCDirectScraper


async def test_detail_id():
    scraper = NSFCDirectScraper(headless=False)
    await scraper.start()
    
    try:
        # 1. 访问搜索页
        print("=" * 50)
        print("1. 访问搜索页...")
        await scraper._page.goto('https://kd.nsfc.cn/finalSearchList?s=人工智能')
        await scraper._page.wait_for_timeout(3000)
        
        # 2. 检查页面状态
        print("\n2. 检查页面状态...")
        url = scraper._page.url
        print(f"   当前URL: {url}")
        
        # 3. 获取页面HTML，查找详情链接
        print("\n3. 分析HTML中的链接...")
        html = await scraper._page.content()
        
        # 查找所有包含 finalDetails 的内容
        detail_patterns = re.findall(r'finalDetails[^"\'>\s]{0,100}', html)
        print(f"   找到 {len(detail_patterns)} 处 finalDetails")
        for p in detail_patterns[:5]:
            print(f"   - {p[:80]}")
        
        # 4. 查找a标签的href
        print("\n4. 检查a标签...")
        a_info = await scraper._page.evaluate('''() => {
            const results = [];
            document.querySelectorAll('a').forEach((a, i) => {
                if (i < 20) {
                    results.push({
                        href: a.href,
                        text: a.innerText.slice(0, 50),
                        onclick: a.getAttribute('onclick')
                    });
                }
            });
            return results;
        }''')
        
        for a in a_info[:10]:
            print(f"   href: {a['href'][:60] if a['href'] else 'None'}")
            print(f"   text: {a['text'][:40]}")
            print(f"   onclick: {a['onclick'][:60] if a['onclick'] else 'None'}")
            print()
        
        # 5. 检查项目卡片结构
        print("\n5. 检查项目卡片结构...")
        card_info = await scraper._page.evaluate('''() => {
            // 查找可能的项目容器
            const selectors = [
                '.info-warp',
                '.project-item',
                '.result-item',
                '[class*="project"]',
                '[class*="item"]'
            ];
            
            const results = {};
            selectors.forEach(sel => {
                const els = document.querySelectorAll(sel);
                results[sel] = els.length;
            });
            
            // 获取第一个项目的HTML结构
            const firstCard = document.querySelector('.info-warp');
            if (firstCard) {
                results['firstCardHTML'] = firstCard.outerHTML.slice(0, 500);
            }
            
            return results;
        }''')
        
        for sel, count in card_info.items():
            if sel != 'firstCardHTML':
                print(f"   {sel}: {count} 个")
        
        if 'firstCardHTML' in card_info:
            print(f"\n   第一个卡片HTML:\n   {card_info['firstCardHTML'][:300]}...")
        
        # 6. 尝试点击第一个项目标题
        print("\n6. 尝试点击项目标题...")
        first_link = await scraper._page.query_selector('.info-warp a')
        if first_link:
            # 获取链接信息
            link_info = await first_link.evaluate('''el => ({
                href: el.href,
                target: el.target,
                onclick: el.getAttribute('onclick')
            })''')
            print(f"   链接href: {link_info['href']}")
            print(f"   链接target: {link_info['target']}")
            print(f"   链接onclick: {link_info['onclick']}")
        else:
            print("   未找到 .info-warp a")
        
    finally:
        await scraper.close()


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