方法一:
# 防止被檢測為自動化工具 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