From 98ef180878875760094c0518f884c06a6fcef88e Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Sun, 5 Jul 2026 00:55:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=20Playwright=20=E5=8F=8D?= =?UTF-8?q?=E7=88=AC=E8=99=AB=E6=A3=80=E6=B5=8B=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加: - 禁用自动化控制特征 - 真实浏览器参数配置 - 反检测脚本注入 - 地理位置等真实特征 - 更强的 User-Agent 模拟 Playwright 后端可以更好地绕过反爬虫检测 --- app.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) 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: