#!/usr/bin/env python3
"""测试衢州页面用Playwright渲染并获取列表"""
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True, args=["--no-sandbox"])
        page = await browser.new_page()
        await page.goto("https://www.qz.gov.cn/col/col1229039395/index.html?number=A09", wait_until="networkidle", timeout=30000)
        
        # 点击"更多"按钮多次加载所有数据
        for i in range(10):
            try:
                more_btn = await page.locator("span.more").first
                if await more_btn.is_visible():
                    await more_btn.click()
                    await page.wait_for_timeout(2000)
                    print(f"第{i+1}次点击更多")
            except:
                print(f"第{i+1}次无更多按钮")
                break
        
        # 提取列表项
        items = await page.evaluate('''
            () => {
                const lis = document.querySelectorAll('ul.ajax-ul li.cf');
                return Array.from(lis).map(li => {
                    const a = li.querySelector('a');
                    const span = li.querySelector('span.fr');
                    return {
                        title: a ? a.getAttribute('title') || a.textContent.trim() : '',
                        date: span ? span.textContent.trim() : '',
                        url: a ? a.href : ''
                    };
                });
            }
        ''')
        print(f"共{len(items)}条")
        for item in items[:5]:
            print(f"  {item['date']} {item['title'][:40]}")
        
        await browser.close()

asyncio.run(main())
