使用Undetected ChromeDriver代理时绕过CloudFlare人机验证方法
如何使用undetected-chromedriver-v2搭配代理绕过CloudFlare验证?
你当前的代码遇到CloudFlare验证,核心问题是代理配置存在疏漏,加上浏览器指纹伪装不够全面,导致被识别为自动化工具。以下是针对性的优化方案:
关键优化点
- 修复代理配置错误:原代码重复设置
https键,需同时配置http和https协议,确保所有请求都走代理 - 强化指纹伪装:补充更多stealth参数,让浏览器指纹更贴近真实用户
- 替换固定等待为显式等待:模拟真实用户的页面加载节奏,避免触发检测
- 增加浏览器启动参数:禁用额外的自动化特征标识
修改后的完整代码
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions import time from selenium_stealth import stealth from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By proxy_id = "proxyid" proxy_password = "proxypw" proxy_ip = "proxyip" proxy_port = "proxyport" def proxy_test(): # 配置代理,同时覆盖http和https协议 seleniumwire_options = { 'proxy': { 'http': f"http://{proxy_id}:{proxy_password}@{proxy_ip}:{proxy_port}", 'https': f"https://{proxy_id}:{proxy_password}@{proxy_ip}:{proxy_port}", 'no_proxy': 'localhost,127.0.0.1' } } # 初始化ChromeOptions,添加额外防检测参数 options = ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--start-maximized") options.add_argument("--disable-extensions") options.add_argument("--disable-plugins-discovery") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = Chrome(options=options, seleniumwire_options=seleniumwire_options) # 强化stealth指纹伪装,补充更多真实浏览器特征 stealth(driver, 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", languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel(R) HD Graphics 630", fix_hairline=True, run_on_insecure_origins=True, ) driver.get("https://csgocases.com/") # 用显式等待代替固定sleep,等待页面核心元素加载 try: WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.TAG_NAME, "body")) ) time.sleep(5) # 留短暂时间让页面完全渲染 except Exception as e: print(f"页面加载超时: {e}") time.sleep(100) driver.quit() def main(): proxy_test() if __name__ == '__main__': main()
额外注意事项
- 确保代理IP的纯净度:避免使用被CloudFlare标记的代理池,优先选择独享或高匿代理
- 匹配指纹与代理地区:如果代理是美国IP,浏览器语言、时区等设置要对应美国地区,避免指纹冲突
- 控制请求频率:模拟真实用户的操作间隔,不要短时间内发送大量请求
内容的提问来源于stack exchange,提问作者ggforces
相关产品推荐
相关产品推荐

