#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""服务器调试: 抓列表页 + 跑 parse_list"""
import re, time, json, sys
sys.path.insert(0, "/root/gov_crawler")
from playwright.sync_api import sync_playwright

IP = "171.221.172.137"
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
LIST_URL = "http://www.pujiang.gov.cn/pjxrmzf/c160163/nav_list.shtml"

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True, args=[
        "--disable-blink-features=AutomationControlled", "--no-sandbox",
        f"--host-resolver-rules=MAP www.pujiang.gov.cn {IP}, MAP pujiang.gov.cn {IP}",
    ])
    ctx = browser.new_context(user_agent=UA, locale="zh-CN", viewport={"width": 1920, "height": 1080})
    page = ctx.new_page()
    try:
        page.goto(LIST_URL, timeout=10000, wait_until="domcontentloaded")
    except Exception as e:
        print("goto:", type(e).__name__)
    html = ""
    for i in range(10):
        time.sleep(2)
        try:
            html = page.content()
        except Exception:
            continue
        if "_ts" not in html and len(html) > 5000:
            print(f"第{i+1}轮: 就绪 {len(html)} bytes")
            break
    # 检查 li 结构
    lis = re.findall(r"<li>(.*?)</li>", html, re.S)
    print(f"li总数: {len(lis)}")
    for li in lis[:3]:
        print("LI:", li.strip()[:150])
    # 检查 span+a
    m = re.findall(r"<li>\s*<span>([^<]*)</span>\s*<a[^>]+href=\"([^\"]+)\"[^>]*>(.*?)</a>", html, re.S)
    print(f"parse_list正则命中: {len(m)}")
    if m:
        print("样本:", m[0])
    browser.close()
