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

使用Selenium Wire+undetected_chromedriver爬取Shopee遭检测求助

问题描述

我正在开发一款Shopee爬虫,单独使用undetected_chromedriver时运行正常,但结合Selenium Wire与undetected_chromedriver后,网站检测到自动化工具并跳转至登录页面。

无法正常运行的代码:

import seleniumwire.undetected_chromedriver as uc 
import time
driver = uc.Chrome()
driver.get('https://shopee.co.id/search?keyword=mobile')
time.sleep(10)
driver.close()

原本可用的代码:

import undetected_chromedriver as uc
import time
driver = uc.Chrome()
driver.get('https://shopee.co.id/search?keyword=mobile')
time.sleep(10)
driver.close()
解决方案

Selenium Wire默认会添加额外代理配置和浏览器启动参数,这些特征会被Shopee的反爬机制识别。可以通过以下方式规避:

  • 自定义Selenium Wire配置,隐藏代理特征
    手动配置seleniumwire_options,禁用默认代理标识,同时继承undetected_chromedriver的优化参数:

    import seleniumwire.undetected_chromedriver as uc
    import time
    
    # 配置Selenium Wire,避免暴露代理特征
    seleniumwire_options = {
        'disable_encoding': True,  # 禁用自动编码,减少特征
        'proxy': {
            'http': None,
            'https': None,
            'no_proxy': 'localhost,127.0.0.1'
        }
    }
    
    # 初始化driver时传入配置
    driver = uc.Chrome(seleniumwire_options=seleniumwire_options)
    driver.get('https://shopee.co.id/search?keyword=mobile')
    time.sleep(10)
    driver.close()
    
  • 手动传递浏览器启动参数
    添加更多模拟正常用户的参数,覆盖Selenium Wire的默认设置:

    import seleniumwire.undetected_chromedriver as uc
    import time
    
    chrome_options = uc.ChromeOptions()
    # 添加反检测参数
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_argument('--disable-extensions')
    chrome_options.add_argument('--profile-directory=Default')
    chrome_options.add_argument('--disable-plugins-discovery')
    chrome_options.add_argument('--start-maximized')
    
    seleniumwire_options = {
        'disable_encoding': True
    }
    
    driver = uc.Chrome(options=chrome_options, seleniumwire_options=seleniumwire_options)
    # 额外移除webdriver标识
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
    driver.get('https://shopee.co.id/search?keyword=mobile')
    time.sleep(10)
    driver.close()
    
  • 强制关闭代理自动配置
    Selenium Wire会自动注入代理导致浏览器指纹异常,初始化时关闭相关自动配置,确保浏览器以正常模式启动。

原理说明

undetected_chromedriver通过修改Chrome启动参数、移除navigator.webdriver标识等方式规避检测,但Selenium Wire底层添加的代理拦截配置会被Shopee反爬系统识别为自动化特征。通过自定义配置隐藏这些特征,就能让浏览器恢复接近正常用户的状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 15:37:52