#!/usr/bin/env python3
"""探测 gddrc: 正文区深挖"""
import asyncio, 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()

        ok = False
        for attempt in range(3):
            try:
                await page.goto("https://drc.gd.gov.cn/gdsnyj/gkmlpt/index#3869", wait_until='domcontentloaded', timeout=60000)
                await page.wait_for_timeout(8000)
                ok = True
                break
            except Exception:
                await page.wait_for_timeout(20000)

        if ok:
            url = "http://drc.gd.gov.cn/gdsnyj/gkmlpt/content/4/4939/post_4939705.html"
            await page.goto(url, wait_until='domcontentloaded', timeout=45000)
            await page.wait_for_timeout(5000)
            # 正文文本: 找"审查意见"正文起始
            text = await page.evaluate("() => document.body.innerText")
            idx = text.find('审查意见')
            print(f"正文起始: {idx}")
            if idx > 0:
                print(f"--- 审查意见 附近 ---")
                print(text[idx:idx+600])
            # 找正文容器 (DOM 里最长文本的 div)
            best = await page.evaluate("""() => {
                let bestEl = null, bestLen = 0;
                document.querySelectorAll('div').forEach(d => {
                    const t = (d.innerText||'').trim();
                    if (t.length > bestLen && t.length > 200) { bestLen = t.length; bestEl = d; }
                });
                return bestEl ? {cls: bestEl.className.slice(0,60), len: bestLen, text: bestEl.innerText.slice(0,300)} : null;
            }""")
            if best:
                print(f"\n最长 div: cls={best['cls']} len={best['len']}")
                print(f"  {best['text'][:200]}")

        await browser.close()

asyncio.run(main())
