selenium反爬-绕过浏览器指纹的常用方法

方法一:去除window.navigator.webdriver特征

代码如下:

from selenium.webdriver import ChromeOptions
from selenium import webdriver

# 实例化对象
option = ChromeOptions()
option.add_experimental_option(‘excludeSwitches’,[‘enable-automation’])# 开启实验性功能

# 去除特征值
option.add_argument(“–disable-blink-features=AutomationControlled”)

# 实例化谷歌
driver = webdriver.Chrome(options=option)
driver.get(“https://www.baidu.com” )

方法二:使用Undetected_chromedriver

Undetected_chromedriver 是一个经过优化的 Selenium WebDriver,可以避免触发反机器人程序。

如绕过 Cloudflare 、Akamai、知乎。它适用于 Google ChromeBrave 和许多其他基于 Chromium 的浏览器。

代码如下:

import undetected_chromedriver as uc
driver = uc.Chrome(use_subprocess=True)
driver.get(“[https://www.baidu.com&#8221](https://www.baidu.com%26/#8221); )

还有一点要注意:

异常现象:

如果使用pyinstallerundetected-chromedriver直接进行打包,那打包后的exe大概率无法运行的。

解决方法:

在代码最开始在import 模块之前加上以下内容,然后再进行打包即可:

from multiprocessing import freeze_support

freeze_support()

原因解析:

在调用某些模块的时候,也是进程,而在多进程中,你程序中的进程不会被阻塞,而一直循环起进程。而undetected-chromedriver内部正好就开了进程。

猜你喜欢

转载自blog.csdn.net/huangbangqing12/article/details/130442417