selenium 反爬蟲識別特征處理


因為業務中發現網站對selenium特征識別為爬蟲了,因此在搜索引擎中搜索進行處理

方式一

# 實例化一個瀏覽器對象
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
if sys.platform == "win32":
    browser = webdriver.Chrome(driver_path, options=options)
else:
    browser = webdriver.Chrome(options=options)
browser.implicitly_wait(30)
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})

此方法雖然可以躲避識別,但是在使用過一段時候后,依然出現被屏蔽的問題。

因此只有繼續搜索 然后再stackoverflow 中又找到了新的答案。

方式二 (推薦)

# 實例化一個瀏覽器對象
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
if sys.platform == "win32":
    browser = webdriver.Chrome(driver_path, options=options)
else:
    browser = webdriver.Chrome(options=options)

這個方法執行的后,目前運行良好也沒有出現被識別的問題。

關閉當前標簽 在切換標簽前關閉

driver.close()
windows = driver.window_handles
driver.switch_to.window(windows[-1])


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM