#!/usr/bin/env python3
"""探测 gddrc: 栏目 tab + 列表加载方式"""
import asyncio, json, re, sys

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"

async def main():
    from playwright.async_api import async_playwright
    from playwright_stealth import Stealth
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            headless=True,
            executable_path='/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome',
            args=['--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled']
        )
        stealth = Stealth()
        ctx = await browser.new_context(user_agent=UA, locale='zh-CN',
                                        viewport={'width': 1440, 'height': 900}, timezone_id='Asia/Shanghai')
        await stealth.apply_stealth_async(ctx)
        page = await ctx.new_page()

        reqs = []
        page.on('request', lambda req: reqs.append((req.method, req.url)) if any(k in req.url for k in ['api', 'list', 'json', 'column', '3869', 'Ajax', 'search']) else None)

        await page.goto("https://drc.gd.gov.cn/gdsnyj/gkmlpt/index#3869", wait_until='domcontentloaded', timeout=45000)
        await page.wait_for_timeout(10000)
        print(f"title: {(await page.title())[:60]}")
        print(f"API 请求 ({len(reqs)}):")
        for m, u in reqs[:15]:
            print(f"  {m} {u[:130]}")

        # 页面里的栏目/tab
        tabs = await page.evaluate("""() => {
            const out = [];
            document.querySelectorAll('a, li, .tab, dt').forEach(el => {
                const t = (el.innerText||'').trim();
                if (t && t.length > 2 && t.length < 25 && /能源|信息|公开|目录|政策|公示|公告|项目|审批/.test(t)) {
                    out.push({t, h: el.href || el.getAttribute('data-id') || '', tag: el.tagName});
                }
            });
            return out.slice(0,25);
        }""")
        seen = set()
        for tb in tabs:
            key = tb['t'] + tb['h']
            if key not in seen:
                seen.add(key)
                print(f"  tab: [{tb['t']}] {tb['tag']} {str(tb['h'])[:80]}")

        # 列表容器
        lst = await page.evaluate("""() => {
            const out = [];
            document.querySelectorAll('ul li').forEach(li => {
                const a = li.querySelector('a');
                if (a) out.push({t: (a.innerText||'').trim().slice(0,40), h: a.href});
            });
            return out.slice(0,10);
        }""")
        print(f"\nli 列表:")
        for it in lst:
            print(f"  [{it['t']}] {it['h'][:90]}")

        await browser.close()

asyncio.run(main())
