selenium反检测方法


方法一:
# 防止被检测为自动化工具

    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(executable_path='./driver/chromedriver.exe', options=options)
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })

 

方法二:

# 浏览器配置对象
options = webdriver.ChromeOptions()
# 以开发者模式启动浏览器
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 屏蔽以开发者运行提示框
# options.add_experimental_option('useAutomationExtension', False)
# 屏蔽保存密码提示框
prefs = {'credentials_enable_service': False, 'profile.password_manager_enabled': False}
options.add_experimental_option('prefs', prefs)
# chrome 88 或更高版本的反爬虫特征处理
options.add_argument('--disable-blink-features=AutomationControlled')
# 浏览器对象
driver = webdriver.Chrome(options=options)
# 读取脚本 下载 stealth.min.js 到本地
with open('stealth.min.js', mode='r', encoding='utf-8') as f:
    string = f.read()
# 移除 selenium 中的爬虫特征
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': string})

 stealth.min.js下载地址:https://github.com/kingname/stealth.min.js


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM