欢迎光临
我们一直在努力

Playwright Python 反检测(反爬)完整实战指南

Playwright Python 反检测(反爬)完整实战指南

适用于:电商上传 / 自动化操作 / 反风控场景
技术栈:Python + Playwright


一、基础环境

pip install playwright
playwright install


二、核心反检测方案

1. 去除 webdriver 标识

from playwright.async_api import async_playwright

async def run():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
context = await browser.new_context()

await context.add_init_script("""
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
"""
)

page = await context.new_page()
await page.goto("https://example.com")

import asyncio
asyncio.run(run())


2. 伪装浏览器环境

context = await browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36",
locale="en-US",
viewport={"width": 1280, "height": 800}
)


3. 注入指纹(plugins / languages)

await context.add_init_script("""
Object.defineProperty(navigator, 'plugins', {
get: () => [1,2,3,4],
});

Object.defineProperty(navigator, 'languages', {
get: () => ['en-US','en'],
});
""")


4. 行为模拟(关键)

鼠标轨迹 + 延迟

import random

await page.mouse.move(100, 200, steps=20)
await page.wait_for_timeout(500 + random.randint(0, 1000))
await page.click("#btn")

滚动页面

await page.mouse.wheel(0, 500)


5. 使用代理 IP

browser = await p.chromium.launch(
proxy={
"server": "http://ip:port",
"username": "xxx",
"password": "xxx"
}
)


6. 持久化浏览器(强烈推荐)

context = await p.chromium.launch_persistent_context(
user_data_dir="./user_data",
headless=False
)


三、完整实战模板(推荐直接用)

import asyncio
import random
from playwright.async_api import async_playwright

async def human_delay(a=1000, b=3000):
await asyncio.sleep(random.uniform(a/1000, b/1000))

async def run():
async with async_playwright() as p:
context = await p.chromium.launch_persistent_context(
user_data_dir="./user_data",
headless=False,
viewport={"width": 1280, "height": 800},
locale="en-US",
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36"
)

page = await context.new_page()

# 注入反检测脚本
await context.add_init_script("""
Object.defineProperty(navigator, 'webdriver', { get: () => false });
Object.defineProperty(navigator, 'plugins', { get: () => [1,2,3] });
Object.defineProperty(navigator, 'languages', { get: () => ['en-US','en'] });
"""
)

await page.goto("https://example.com")

# 模拟人类行为
await human_delay()
await page.mouse.move(200, 300, steps=30)
await human_delay()
await page.mouse.wheel(0, 600)
await human_delay()

await page.click("text=Login")

await human_delay(2000, 5000)

# 输入
await page.fill("#username", "test")
await human_delay()
await page.fill("#password", "123456")

await human_delay()
await page.click("#submit")

await human_delay(3000, 6000)

await context.close()

asyncio.run(run())


四、防封核心策略

1. 账号隔离

  • 一个账号 = 一个 IP
  • 一个账号 = 一个浏览器目录

2. 控制频率

await page.wait_for_timeout(3000 + random.randint(0, 5000))

3. 模拟真实路径

错误:

  • 直接进入发布页

正确:

  • 首页 → 浏览 → 搜索 → 停留 → 操作

4. 半自动方案(最稳)

推荐:

  • 自动填写
  • 人工点击提交

五、进阶建议

  • 使用住宅 IP
  • 不要批量并发跑
  • 不要同一 IP 多账号
  • 每个账号保持独立行为轨迹

六、总结

Playwright 反检测核心三点:

  • 浏览器指纹伪装
  • 行为像真人
  • 网络环境干净
  • 👉 只要做到这三点,稳定性会大幅提升


    赞(0)
    未经允许不得转载:171主机测评 » Playwright Python 反检测(反爬)完整实战指南
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址