#!/usr/bin/env python3
"""探测 hjbh 详情页正文容器"""
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()

        await page.goto("http://cnqc.gov.cn/", wait_until='domcontentloaded', timeout=45000)
        await page.wait_for_timeout(12000)

        # 从 hjbh 列表拿一个详情 URL
        await page.goto("http://cnqc.gov.cn/gongkai/list/20220811145224789.html?page=1&t=31", wait_until='domcontentloaded', timeout=40000)
        await page.wait_for_timeout(5000)
        html = await page.content()
        rows = re.findall(r'<a[^>]*href="([^"]*show[^"]*\.html)"[^>]*title="([^"]*)"', html)
        print(f"列表项: {len(rows)}")
        for u, t in rows[:3]:
            print(f"  {u} | {t[:40]}")

        if rows:
            url = rows[0][0] if rows[0][0].startswith('http') else 'http://cnqc.gov.cn' + rows[0][0]
            print(f"\n[详情] {url}")
            r = await page.goto(url, wait_until='domcontentloaded', timeout=40000)
            await page.wait_for_timeout(5000)
            dh = await page.content()
            print(f"status={r.status if r else '?'} len={len(dh)} title={(await page.title())[:60]}")
            # 找所有 class
            from collections import Counter
            divs = re.findall(r'<div[^>]*class="([^"]{3,60})"[^>]*>', dh)
            c = Counter(divs)
            print("\ndiv classes:")
            for cls, n in c.most_common(25):
                print(f"  div.{cls} x{n}")
            # 长文本容器
            print("\n--- 长文本容器 ---")
            for cls in c:
                if c[cls] == 1:
                    idx = dh.find(f'class="{cls}"')
                    if idx > 0:
                        start = dh.find('>', idx) + 1
                        text = re.sub(r'<[^>]+>', '', dh[start:start+400]).strip()
                        if len(text) > 40:
                            print(f"  [{cls}] {text[:70]}")

        await browser.close()

asyncio.run(main())
