81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Check page content"""
|
|
|
|
import sys
|
|
sys.path.insert(0, '/home/starmen1265/work/kb-skills')
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
from kb_workflow.pipeline.config import get_settings
|
|
|
|
# Get cookie
|
|
settings = get_settings()
|
|
cookie_str = settings.zhihu_cookie.get_secret_value()
|
|
|
|
# Parse cookies
|
|
cookies = []
|
|
for item in cookie_str.split(';'):
|
|
item = item.strip()
|
|
if '=' in item:
|
|
name, value = item.split('=', 1)
|
|
cookies.append({
|
|
'name': name.strip(),
|
|
'value': value.strip(),
|
|
'domain': '.zhihu.com',
|
|
'path': '/',
|
|
'secure': True,
|
|
'httpOnly': False,
|
|
'sameSite': 'Lax'
|
|
})
|
|
|
|
# Test with browser
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
context = browser.new_context(
|
|
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
service_workers='block' # Block service workers
|
|
)
|
|
|
|
# Add cookies
|
|
context.add_cookies(cookies)
|
|
|
|
# Create page
|
|
page = context.new_page()
|
|
|
|
url = "https://www.zhihu.com/question/2016993095078684011/answer/2017037562368246182"
|
|
print(f"Navigating to: {url}")
|
|
|
|
# Log requests
|
|
def log_request(request):
|
|
if "/api/v4/" in request.url:
|
|
print(f" Request: {request.method} {request.url[:100]}")
|
|
|
|
page.on("request", log_request)
|
|
|
|
# Log responses
|
|
def log_response(response):
|
|
if "/api/v4/" in response.url:
|
|
print(f" Response: {response.status} {response.url[:100]}")
|
|
|
|
page.on("response", log_response)
|
|
|
|
# Navigate
|
|
page.goto(url, wait_until="networkidle", timeout=60000)
|
|
|
|
print(f"\nPage title: {page.title()}")
|
|
print(f"Page URL: {page.url}")
|
|
|
|
# Wait a bit more
|
|
import time
|
|
time.sleep(3)
|
|
|
|
# Get content
|
|
content = page.content()
|
|
print(f"\nPage content length: {len(content)}")
|
|
print(f"First 500 chars:\n{content[:500]}")
|
|
|
|
# Check for errors
|
|
if "error" in content.lower() or "404" in content or "403" in content:
|
|
print("\n⚠️ Possible error in page")
|
|
|
|
browser.close()
|