Gloomy's Blog

Gloomy's Blog Website

0%

Python selenium 调用 隐藏指纹特征

<Excerpt in index | 首页摘要>

Python selenium 调用 隐藏指纹特征

<The rest of contents | 余下全文>

为什么要隐藏?

很多网址做了大量的反扒行为 为了方便绕过这个检测 需要做指纹特征的隐藏

检测当前浏览器指纹特征 https://bot.sannysoft.com/

如何隐藏

核心就是隐藏浏览器navigator.webdriver 这个对象

然后加上User-Agent等尽可能像是普通用户

再加上一个小技巧, 如果目标网站提供Mobile版本 通常爬Mobile站会容易许多..

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("incognito")
chrome_options.add_argument("disable-extensions")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/91.0.4472.124 Safari/537.36")
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)

browser = webdriver.Chrome(executable_path="C:\\Users\\gloomy\\Downloads\\chromedriver_win32\\chromedriver.exe",
options=chrome_options)
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
browser.set_window_size(400, 785)
titleEl = browser.find_element_by_class_name("title-content")
print(titleEl.text)
print(titleEl.text)
browser.close()