增强 Playwright 反爬虫检测能力
添加: - 禁用自动化控制特征 - 真实浏览器参数配置 - 反检测脚本注入 - 地理位置等真实特征 - 更强的 User-Agent 模拟 Playwright 后端可以更好地绕过反爬虫检测
This commit is contained in:
39
app.py
39
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:
|
||||
|
||||
Reference in New Issue
Block a user