#!/usr/bin/env python3
"""探测 hngy: 详情页正文容器"""
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("https://www.hngy.gov.cn/zwgk/zwdt/tzgg/default.htm", wait_until='domcontentloaded', timeout=45000)
        await page.wait_for_timeout(5000)

        det = "https://www.hngy.gov.cn/zwgk/zwdt/tzgg/content_4029991.html"
        r = await page.goto(det, wait_until='domcontentloaded', timeout=35000)
        await page.wait_for_timeout(4000)
        dh = await page.content()
        print(f"status={r.status if r else '?'} len={len(dh)}")
        # 找含正文的 div class
        divs = re.findall(r'<div[^>]*class="([^"]{3,60})"[^>]*>', dh)
        # 统计出现次数
        from collections import Counter
        c = Counter(divs)
        for cls, n in c.most_common(20):
            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+500]).strip()
                    if len(text) > 50:
                        print(f"  [{cls}] {text[:80]}")

        await browser.close()

asyncio.run(main())
