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

使用Undetected Selenium绕过ceo.baemin.com Bot检测遇阻求助

解决Headless模式下访问https://ceo.baemin.com被Bot检测拦截的问题

依赖安装

先执行命令安装所需依赖:

pip install fake_useragent undetected_chromedriver selenium_stealth selenium

你的代码存在几个导致headless模式被检测的问题,比如旧版headless特征明显、参数添加时机错误、UA固定过时等,以下是修复后的完整代码:

修复后的代码示例

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
from fake_useragent import UserAgent
from selenium_stealth import stealth
from selenium.webdriver.chrome.service import Service
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

options = uc.ChromeOptions()

# 基础防检测配置
options.add_argument('--disable-popup-blocking')
options.add_argument('--incognito')
options.add_argument('--no-sandbox')
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

# Headless模式核心配置,必须添加
options.add_argument('--headless=new')  # 新版headless更接近正常浏览器行为
options.add_argument('--window-size=1920,1080')  # 模拟桌面窗口大小,避免小窗口被标记
options.add_argument('--disable-gpu')
options.add_argument('--remote-debugging-port=9222')  # 提前加入options,初始化后添加无效

# 生成匹配当前Chrome版本的随机UA,避免固定过时UA
ua = UserAgent()
options.add_argument(f'user-agent={ua.chrome}')

# 初始化driver,移除重复的incognito参数(options已配置)
driver = uc.Chrome(
    options=options,
    enable_cdp_events=True,
    use_subprocess=True
)

# 优化stealth配置,贴近真实浏览器环境
stealth(driver,
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel(R) Iris(R) Xe Graphics",  # 常见显卡型号,减少异常特征
        fix_hairline=True,
        run_on_insecure_origins=True,  # 兼容非安全源页面
        languages=["en-US", "en"]
        )

# 页面加载前注入脚本,确保所有页面都覆盖webdriver等属性
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
        Object.defineProperty(navigator, 'webdriver', {
            get: () => undefined
        });
        Object.defineProperty(navigator, 'plugins', {
            get: () => [1, 2, 3, 4, 5]
        });
        window.chrome = {
            runtime: {}
        };
    """
})

# 双重保险:通过CDP命令覆盖UA
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": ua.chrome})

driver.implicitly_wait(5)  # 延长等待时间,确保页面加载完成

driver.get('https://ceo.baemin.com')

print(driver.page_source)

关键修改点说明

  • 升级Headless模式:用--headless=new替代旧版headless,新版Chrome的headless模式行为和正常浏览器几乎一致,大幅降低被检测概率
  • 修正参数添加时机:--remote-debugging-port这类参数必须在初始化driver前添加到options,否则不会生效
  • 动态UA生成:使用fake_useragent生成匹配当前Chrome版本的UA,避免固定旧版UA被识别为异常
  • 提前注入脚本:通过Page.addScriptToEvaluateOnNewDocument在页面加载前注入脚本,确保所有页面(包括跳转后的)都能覆盖webdriver等敏感属性
  • 优化Stealth参数:更新显卡渲染器为常见型号,添加run_on_insecure_origins参数增强兼容性
  • 移除重复配置:删除driver初始化时重复的incognito=True,避免配置冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 16:15:08