增强 Playwright 反爬虫检测能力

添加:
- 禁用自动化控制特征
- 真实浏览器参数配置
- 反检测脚本注入
- 地理位置等真实特征
- 更强的 User-Agent 模拟

Playwright 后端可以更好地绕过反爬虫检测
This commit is contained in:
2026-07-05 00:55:09 +08:00
parent 6b7e856e38
commit 98ef180878

39
app.py
View File

@@ -194,26 +194,59 @@ async def capture_with_playwright(
): ):
""" """
使用 Playwright 捕获网页 使用 Playwright 捕获网页
Playwright 有更强的反爬虫能力
""" """
if not PLAYWRIGHT_AVAILABLE: 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] session_id = str(uuid.uuid4())[:8]
try: try:
async with async_playwright() as p: 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} viewport_settings = viewport or {"width": 1920, "height": 1080}
# 创建上下文,模拟真实浏览器
context = await browser.new_context( context = await browser.new_context(
viewport={ viewport={
"width": viewport_settings.get("width", 1920), "width": viewport_settings.get("width", 1920),
"height": viewport_settings.get("height", 1080) "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() page = await context.new_page()
try: try: