如何在无头模式下用Undetected Chrome绕过Cloudflare检测?
解决方案:Undetected Chrome无头模式规避Cloudflare拦截
针对你的问题,结合Undetected Chrome(uc)的特性,需要调整以下关键配置来绕过Cloudflare的机器人检测,实现无头模式下的正常访问:
核心配置要点
- 使用新无头模式:Chrome 113支持
--headless=new参数,该模式的行为更接近真实的有头浏览器,大幅降低被检测概率,避免旧--headless的指纹特征。 - 补充真实浏览器参数:设置合理的窗口大小、禁用GPU、禁用不必要的扩展,模拟真实用户环境。
- 正确配置
selenium_stealth:覆盖常见的自动化检测点,比如navigator.webdriver、window.chrome等特征。 - 匹配版本的User Agent:确保UA与Chrome 113版本一致,避免特征不匹配。
修改后的完整代码
from selenium import webdriver from selenium_stealth import stealth import undetected_chromedriver as uc def getDriver(): options = uc.ChromeOptions() # 启用新无头模式(关键配置) options.add_argument("--headless=new") # 设置窗口大小,模拟真实用户浏览窗口 options.add_argument("--window-size=1920,1080") # 禁用GPU,避免无头模式下的异常特征 options.add_argument("--disable-gpu") # 禁用扩展和插件扫描 options.add_argument("--disable-extensions") options.add_argument("--disable-plugins-discovery") # 屏蔽自动化相关提示与扩展 options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) # 添加Chrome 113匹配的User Agent user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" options.add_argument(f"user-agent={user_agent}") # 禁用WebRTC,减少指纹泄露风险 options.add_argument("--disable-webrtc") # 设置浏览器语言,模拟真实用户环境 options.add_argument("--lang=en-US,en;q=0.9") # 初始化Undetected Chrome驱动 browser = uc.Chrome(options=options, version_main=113) # 配置selenium_stealth,覆盖自动化检测特征 stealth(browser, languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL Engine", fix_hairline=True, run_on_insecure_origins=True ) return browser
额外注意事项
- 会话复用:同一浏览器实例打开新标签页可成功,建议复用驱动实例,避免频繁创建新会话,减少检测触发频率。
- 请求频率控制:每日1-2万次访问需合理设置请求间隔,模拟真实用户的浏览节奏,避免短时间内大量请求触发Cloudflare阈值。
- 缓存维持:保留浏览器缓存与会话Cookie,有助于维持已通过验证的访问状态。
内容的提问来源于stack exchange,提问作者Ting Darren
相关产品推荐
相关产品推荐

