#!/usr/bin/env python3
"""检查 yanshougs.com 第2页返回内容"""
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()
        
        # 先访问首页设置session
        await page.goto('https://www.yanshougs.com/', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(2000)
        cookies = await page.context.cookies()
        print(f"首页后 cookies: {len(cookies)}")
        
        # 访问环评公示第1页
        await page.goto('https://www.yanshougs.com/list/4.html', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(3000)
        print(f"\n环评公示第1页 URL: {page.url}")
        
        # 获取第1页表格行数
        rows1 = await page.evaluate("document.querySelectorAll('table tbody tr').length")
        print(f"第1页表格行数: {rows1}")
        
        # 检查分页总页数
        total_pages = await page.evaluate("""() => {
            const links = document.querySelectorAll('.pagination a');
            let max = 0;
            links.forEach(a => {
                const n = parseInt(a.textContent);
                if (!isNaN(n) && n > max) max = n;
            });
            return max;
        }""")
        print(f"环评公示总页数: {total_pages}")
        
        # 模拟翻页 - 点击第2页
        page2_link = await page.query_selector('.pagination a[href="/publicity_list?page=2"]')
        if page2_link:
            # 先获取href
            href = await page2_link.get_attribute('href')
            print(f"\n翻页href: {href}")
        
        # 直接导航到第2页
        print("\n--- 导航到第2页 ---")
        await page.goto('https://www.yanshougs.com/publicity_list?page=2', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(5000)
        print(f"第2页 URL: {page.url}")
        
        rows2 = await page.evaluate("document.querySelectorAll('table tbody tr').length")
        print(f"第2页表格行数: {rows2}")
        
        # 查看第2页表格内容
        items2 = await page.evaluate("""() => {
            const rows = document.querySelectorAll('table tbody tr');
            return Array.from(rows).slice(0,3).map(r => {
                const cells = r.querySelectorAll('td');
                if (cells.length < 2) return 'no cells';
                const link = cells[0].querySelector('a[href]');
                const href = link ? link.getAttribute('href') : 'no link';
                const title = cells[0].textContent.trim().substring(0, 50);
                return {title: title, href: href};
            });
        }""")
        print(f"第2页前3条:")
        for i, item in enumerate(items2):
            print(f"  #{i+1}: {item['href']} | {item['title']}")
        
        # 查看当前位置的分页
        pagination2 = await page.evaluate("""() => {
            const links = document.querySelectorAll('.pagination a');
            return Array.from(links).map(a => ({
                text: a.textContent.trim(),
                href: a.getAttribute('href'),
                cls: a.className
            }));
        }""")
        print(f"\n第2页分页: {pagination2[:5]}")
        
        await browser.close()

asyncio.run(main())
