简介

最近在写自动登录 QQ 音乐刷新 Cookie 的工具,发现就算破解了滑块还是不能成功登录,可能是 QQ 音乐检测到了 Selenium。
这个方法是在网上看到的,记录一下。
是使用 stealth.min.js 来反反爬虫

Python 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
# 2,3和5~11行是网上找的另一种反检测的方法,也记在这里

with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
driver.get(url)

获取 stealth.min.js

Github

Github 里并没有直接提供 stealth.min.js 需要自己生成。
我试着跑了一下代码,但是报错了,没能解决。
这里贴一下错误

这是我在网上找到的 stealth.min.js

下载