selenium爬虫如何防止被浏览器特征抓取反爬,undetected_chromedriver他来了。

undetected_chromedriver 可以防止浏览器特征被识别,并且可以根据浏览器版本自动下载驱动。

安装方式:

pip install  git+https://github.com/ultrafunkamsterdam/undetected-chromedriver.git

一个简单的例子 ,懂车帝对selenium反爬挺厉害,通过undetected_chromedriver可轻松搞定。

import undetected_chromedriver as uc
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

uc.TARGET_VERSION = 91
driver = uc.Chrome()
driver.get('https://www.dongchedi.com/user/53334173333')
driver.find_element_by_xpath('//*[@id="__next"]/div[1]/div[2]/div/div[1]/div[2]/header/nav/a[2]').click()
driver.close()

还支持自定义功能

import undetected_chromedriver as uc

#specify chromedriver version to download and patch
#this did not work correctly until 1.2.1
uc.TARGET_VERSION = 78    

# or specify your own chromedriver binary to patch
undetected_chromedriver.install(
    executable_path='c:/users/user1/chromedriver.exe',
)
from selenium.webdriver import Chrome, ChromeOptions
opts = ChromeOptions()
opts.add_argument(f'--proxy-server=socks5://127.0.0.1:9050')
driver = Chrome(options=opts)
driver.get('https://distilnetworks.com')

猜你喜欢

转载自blog.csdn.net/wywinstonwy/article/details/118479162#comments_21033695
今日推荐