diff --git a/app.py b/app.py index c8f4915..2d16f9d 100644 --- a/app.py +++ b/app.py @@ -194,26 +194,59 @@ async def capture_with_playwright( ): """ 使用 Playwright 捕获网页 + Playwright 有更强的反爬虫能力 """ if not PLAYWRIGHT_AVAILABLE: - return {"success": False, "error": "Playwright not installed"} + return {"success": False, "error": "Playwright not installed. Run: pip install playwright && playwright install chromium"} session_id = str(uuid.uuid4())[:8] try: async with async_playwright() as p: - browser = await p.chromium.launch(headless=True) + # 启动浏览器,添加反检测配置 + browser = await p.chromium.launch( + headless=True, + args=[ + '--disable-blink-features=AutomationControlled', # 反自动化检测 + '--disable-dev-shm-usage', + '--no-sandbox', + '--disable-setuid-sandbox', + '--disable-web-security', + ] + ) viewport_settings = viewport or {"width": 1920, "height": 1080} + # 创建上下文,模拟真实浏览器 context = await browser.new_context( viewport={ "width": viewport_settings.get("width", 1920), "height": viewport_settings.get("height", 1080) }, - user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" + user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + # 添加更多浏览器特征 + locale='zh-CN', + timezone_id='Asia/Shanghai', + geolocation={'latitude': 31.2304, 'longitude': 121.4737}, # 上海坐标 + permissions=['geolocation'], ) + # 注入反检测脚本 + await context.add_init_script(""" + Object.defineProperty(navigator, 'webdriver', { + get: () => undefined + }); + Object.defineProperty(navigator, 'plugins', { + get: () => [1, 2, 3, 4, 5] + }); + Object.defineProperty(navigator, 'languages', { + get: () => ['zh-CN', 'zh', 'en', 'en-US'] + }); + window.chrome = { + runtime: {} + }; + """) + page = await context.new_page() try: