#!/usr/bin/env python3
"""检查 yanshougs.com 分页URL格式"""
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as pw:
        browser = await pw.chromium.launch(headless=True, args=['--no-sandbox'])
        page = await browser.new_page()
        
        # 检查环评公示第1页的分页链接
        await page.goto('https://www.yanshougs.com/list/4.html', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(3000)
        
        pagination = await page.evaluate("""() => {
            const sel = document.querySelector('.pagination');
            if (!sel) return 'no pagination';
            const links = sel.querySelectorAll('a');
            return Array.from(links).map(a => ({
                text: a.textContent.trim(),
                href: a.getAttribute('href')
            }));
        }""")
        print('环评公示(id=4) 第1页 分页:', pagination)
        
        # 检查水保验收第1页
        await page.goto('https://www.yanshougs.com/list/2.html', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(3000)
        pagination2 = await page.evaluate("""() => {
            const sel = document.querySelector('.pagination');
            if (!sel) return 'no pagination';
            const links = sel.querySelectorAll('a');
            return Array.from(links).map(a => ({
                text: a.textContent.trim(),
                href: a.getAttribute('href')
            }));
        }""")
        print('水保验收(id=2) 第1页 分页:', pagination2)
        
        # 直接尝试 publicity_list 带类别参数
        await page.goto('https://www.yanshougs.com/publicity_list?page=2&cat=4', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(3000)
        url2 = page.url
        print(f'\\n尝试 /publicity_list?page=2&cat=4 实际URL: {url2}')
        
        table2 = await page.evaluate("""() => {
            const rows = document.querySelectorAll('table tbody tr');
            if (rows.length === 0) return 'no rows';
            return Array.from(rows).slice(0,3).map(r => {
                const cells = r.querySelectorAll('td');
                return cells.length > 0 ? cells[0].textContent.trim().substring(0,40) : 'empty';
            });
        }""")
        print(f'表格行数: {len(table2) if isinstance(table2, list) else table2}')
        if isinstance(table2, list):
            for i, t in enumerate(table2):
                print(f'  行{i+1}: {t}')
        
        await browser.close()

asyncio.run(main())
