You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium爬取时遭遇Cloudflare人机验证,求可行绕过方案

绕过Cloudflare人机验证的可行方案

1. 优化undetected_chromedriver配置

普通ChromeDriver极易被Cloudflare识别,undetected_chromedriver需要正确配置以规避自动化检测,以下是调整后的代码:

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 配置浏览器选项,屏蔽自动化特征
options = uc.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

# 初始化driver,自动匹配Chrome版本
driver = uc.Chrome(options=options)

url = 'target'
driver.get(url)

# 等待Cloudflare验证完成,显式等待比sleep更可靠
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, "userLoginForm.userId")))

# 后续登录操作
username_input = driver.find_element(By.ID, "userLoginForm.userId")
username_input.send_keys("username")

password_input = driver.find_element(By.ID, "userLoginForm.userPasswd")
password_input.send_keys("password")

loginBtn = driver.find_element(By.XPATH, '//*[@id="js-main"]/div/div[2]/div/input')
loginBtn.click()

注意:确保undetected_chromedriver是最新版本,避免Chrome版本不兼容问题。

2. 代理IP+指纹伪装组合

Cloudflare会通过IP请求频率、浏览器指纹识别爬虫,建议使用住宅代理(而非数据中心代理),同时在配置中添加更多真实用户的浏览器特征:

# 在options中添加以下参数
options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
options.add_argument("--disable-extensions")
options.add_argument("--disable-plugins-discovery")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

可以通过第三方库(如fake_useragent)随机生成User-Agent,进一步降低指纹辨识度。

3. 手动验证后复用会话Cookie

若之前Cookie欺骗失败,大概率是未获取完整有效Cookie或IP不匹配。操作步骤:

  1. 手动打开Chrome,访问目标网站完成Cloudflare验证和登录
  2. 通过浏览器开发者工具(F12)导出所有Cookie,重点保留cf_clearance和会话相关Cookie
  3. 在代码中导入Cookie并复用会话:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

service = Service(executable_path='./chromedriver')
driver = webdriver.Chrome(service=service)

# 先访问目标网站,确保域名匹配
driver.get('target')

# 替换为你手动获取的Cookie
cookies = [
    {'name': 'cf_clearance', 'value': 'YOUR_CF_CLEARANCE_VALUE', 'domain': '.your-target-domain.com'},
    {'name': 'SESSION_ID', 'value': 'YOUR_SESSION_VALUE', 'domain': '.your-target-domain.com'},
    # 添加其他必要Cookie
]
for cookie in cookies:
    driver.add_cookie(cookie)

# 刷新页面,此时应已跳过验证
driver.refresh()

# 若会话未包含登录状态,执行后续登录操作
username_input = driver.find_element(By.ID, "userLoginForm.userId")
username_input.send_keys("username")

password_input = driver.find_element(By.ID, "userLoginForm.userPasswd")
password_input.send_keys("password")

loginBtn = driver.find_element(By.XPATH, '//*[@id="js-main"]/div/div[2]/div/input')
loginBtn.click()

⚠️ 注意:cf_clearance与IP绑定,必须使用获取Cookie时的同一IP,否则Cookie无效。

4. 模拟真实用户行为,降低请求频率

Cloudflare会检测异常的操作速度,在代码中添加随机延迟,模拟真实用户输入节奏:

import time
import random

# 页面加载后随机等待2-5秒
time.sleep(random.uniform(2, 5))

# 逐个字符输入用户名,模拟真实操作
username_input = driver.find_element(By.ID, "userLoginForm.userId")
for char in "username":
    username_input.send_keys(char)
    time.sleep(random.uniform(0.1, 0.3))

# 同理处理密码输入
password_input = driver.find_element(By.ID, "userLoginForm.userPasswd")
for char in "password":
    password_input.send_keys(char)
    time.sleep(random.uniform(0.1, 0.3))

# 点击登录前随机等待1-3秒
time.sleep(random.uniform(1, 3))
loginBtn.click()

内容的提问来源于stack exchange,提问作者hiroshi kohama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 09:10:11