问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

最全总结 | 聊聊 Selenium 隐藏浏览器指纹特征的几种方式

创作时间:
作者:
@小白创作中心

最全总结 | 聊聊 Selenium 隐藏浏览器指纹特征的几种方式

引用
CSDN
1.
https://blog.csdn.net/2401_85726131/article/details/141501985

在使用Selenium进行网页爬虫时,如何隐藏浏览器指纹特征是一个常见的问题。本文将介绍几种常用的解决方案,帮助你应对网站的反爬机制。

当我们使用Selenium对网页进行爬虫时,如果不做任何处理直接进行爬取,会导致很多特征暴露。对于一些做了反爬的网站,这些特征会被用来阻止恶意爬虫。本文将介绍几种常用的隐藏浏览器指纹特征的方式。

1. 直接爬取

目标对象:

aHR0cHM6Ly9xaWthbi5jcXZpcC5jb20vUWlrYW4vU2VhcmNoL0FkdmFuY2U=

我们使用Selenium直接爬取目标页面:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time

chrome_options = Options()
s = Service(r"chromedriver.exe路径")
driver = webdriver.Chrome(service=s, options=chrome_options)
driver.get(url='URL')
driver.save_screenshot('result.png')

source = driver.page_source
with open('result.html', 'w') as f:
    f.write(source)

time.sleep(200)

页面明显做了反爬,网页返回直接返回空白内容。

2. CDP

CDP全称为Chrome Devtools-Protocol,通过执行CDP命令,可以在网页加载前运行一段代码,进而改变浏览器的指纹特征。比如,window.navigator.webdriver在Selenium直接打开网页时返回结果为true;而手动打开网页时,该对象值为undefined。因此,我们可以利用CDP命令修改该对象的值,达到隐藏指纹特征的目的。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time

chrome_options = Options()
s = Service(r"chromedriver.exe路径")
driver = webdriver.Chrome(service=s, options=chrome_options)

# 执行cdp命令,修改(window.navigator.webdriver )对象的值
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
        Object.defineProperty(navigator, 'webdriver', {
            get: () => undefined
        })
    """
})

driver.get(url='URL')
driver.save_screenshot('result.png')

source = driver.page_source
with open('result.html', 'w', encoding='utf-8') as f:
    f.write(source)

time.sleep(200)

需要指出的是,浏览器的指纹特征很多,使用该方法存在一些局限性。

3. stealth.min.js

该文件包含了常用的浏览器特征,我们只需要读取该文件,然后执行CDP命令即可。下载地址:https://github.com/berstend/puppeteer-extra/tree/stealth-js

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

chrome_options = Options()
# 无头模式
# chrome_options.add_argument("--headless")
# 添加请求头
chrome_options.add_argument(
    'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36')

s = Service(r"chromedriver.exe路径")
driver = webdriver.Chrome(service=s, options=chrome_options)

# 利用stealth.min.js隐藏浏览器指纹特征
# stealth.min.js下载地址:https://github.com/berstend/puppeteer-extra/tree/stealth-js
with open('./stealth.min.js') as f:
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": f.read()
    })

driver.get(url='URL')
# driver.get(url='https://bot.sannysoft.com/')
# 保存图片
driver.save_screenshot('result.png')

time.sleep(200)

4. undetected_chromedriver

这是一个防止浏览器指纹特征被识别的依赖库,可以自动下载驱动配置再运行。项目地址:https://github.com/ultrafunkamsterdam/undetected-chromedriver

使用步骤也很方便,首先安装依赖库:

pip3 install undetected-chromedriver

然后,通过下面几行代码就能完美隐藏浏览器的指纹特征:

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
import undetected_chromedriver as uc

chrome_options = Options()
# chrome_options.add_argument("--headless")
s = Service(r"chromedriver.exe")
driver = uc.Chrome(service=s, options=chrome_options)
driver.get(url='URL')
# driver.get(url='https://bot.sannysoft.com/')
driver.save_screenshot('result.png')

time.sleep(100)

5. 操作已开启的浏览器

最后一种方式是通过命令行启动一个浏览器,然后利用Selenium直接操作上面的浏览器,模拟正常操作浏览器的行为。

首先,通过命令行启动一个浏览器:

import subprocess

# 1、打开浏览器
# 指定端口号为:1234
# 配置用户数据路径:--user-data-dir
cmd = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe --remote-debugging-port=1234 --user-data-dir="C:\\selenum\\user_data"'
subprocess.run(cmd)

然后,利用Selenium直接操作上面的浏览器:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

# 操作上面已经打开的浏览器,进行百度搜索
chrome_options = Options()
# 指定已经打开浏览器的地址及端口号
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1234")
# 注意:chrome版本与chromedirver驱动要保持一致
# 下载地址:http://chromedriver.storage.googleapis.com/index.html
s = Service(r"chromedriver.exe")
driver = webdriver.Chrome(service=s, options=chrome_options)

# 打开目标网站
driver.get(url="URL")
time.sleep(200)

6. 最后

上面罗列出了多种应对网站反爬的解决方案,大家可以根据实际需求去选择适合自己的方案。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号