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

在这里插入图片描述

我们使用 Selenium 对网页进行爬虫时,如果不做任何处理直接进行爬取,会导致很多特征是暴露的

对一些做了反爬的网站,做了特征检测,用来阻止一些恶意爬虫

本篇文章将介绍几种常用的隐藏浏览器指纹特征的方式

1. 直接爬取

目标对象:

aHR0cHM6Ly9xaWthbi5jcXZpcC5jb20vUWlrYW4vU2VhcmNoL0FkdmFuY2U=

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

  1. # selenium 直接爬取

  2. from selenium import webdriver

  3. from selenium.webdriver.chrome.options import Options

  4. from selenium.webdriver.chrome.service import Service

  5. import time

  6. chrome_options = Options()

  7. s = Service(r"chromedriver.exe路径")

  8. driver = webdriver.Chrome(service=s, options=chrome_options)

  9. driver.get(url='URL')

  10. driver.save_screenshot('result.png')

  11. # 保存

  12. source = driver.page_source

  13. with open('result.html', 'w') as f:

  14. f.write(source)

  15. time.sleep(200)

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

图片

2. CDP

CDP 全称为 Chrome Devtools-Protocol

https://chromedevtools.github.io/devtools-protocol/

通过执行 CDP 命令,可以在网页加载前运行一段代码,进而改变浏览器的指纹特征

比如,window.navigator.webdriver 在 Selenium 直接打开网页时返回结果为 true;而手动打开网页时,该对象值为 undefined

因此,我们可以利用 CDP 命令修改该对象的值,达到隐藏指纹特征的目的

  1. from selenium import webdriver

  2. from selenium.webdriver.chrome.options import Options

  3. from selenium.webdriver.chrome.service import Service

  4. import time

  5. chrome_options = Options()

  6. s = Service(r"chromedriver.exe路径")

  7. driver = webdriver.Chrome(service=s, options=chrome_options)

  8. # 执行cdp命令,修改(window.navigator.webdriver )对象的值

  9. driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {

  10. "source": """

  11. Object.defineProperty(navigator, 'webdriver', {

  12. get: () => undefined

  13. })

  14. """

  15. })

  16. driver.get(url='URL')

  17. driver.save_screenshot('result.png')

  18. # 保存

  19. source = driver.page_source

  20. with open('result.html', 'w', encoding='utf-8') as f:

  21. f.write(source)

  22. time.sleep(200)

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

3. stealth.min.js

该文件包含了常用的浏览器特征,我们只需要读取该文件,然后执行 CDP 命令即可

下载地址:

https://github.com/berstend/puppeteer-extra/tree/stealth-js

 
  1. from selenium import webdriver

  2. from selenium.webdriver.chrome.options import Options

  3. from selenium.webdriver.chrome.service import Service

  4. from selenium.webdriver.common.by import By

  5. import time

  6. chrome_options = Options()

  7. # 无头模式

  8. # chrome_options.add_argument("--headless")

  9. # 添加请求头

  10. chrome_options.add_argument(

  11. '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')

  12. s = Service(r"chromedriver.exe路径")

  13. driver = webdriver.Chrome(service=s, options=chrome_options)

  14. # 利用stealth.min.js隐藏浏览器指纹特征

  15. # stealth.min.js下载地址:https://github.com/berstend/puppeteer-extra/tree/stealth-js

  16. with open('./stealth.min.js') as f:

  17. driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {

  18. "source": f.read()

  19. })

  20. driver.get(url='URL')

  21. # driver.get(url='https://bot.sannysoft.com/')

  22. # 保存图片

  23. driver.save_screenshot('result.png')

  24. time.sleep(200)

4. undetected_chromedriver

这是一个防止浏览器指纹特征被识别的依赖库,可以自动下载驱动配置再运行

项目地址:

https://github.com/ultrafunkamsterdam/undetected-chromedriver
使用步骤也很方便

首先,我们安装依赖库

  1. # 安装依赖

  2. pip3 install undetected-chromedriver

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

  1. from selenium.webdriver.chrome.options import Options

  2. from selenium.webdriver.chrome.service import Service

  3. import time

  4. import undetected_chromedriver as uc

  5. chrome_options = Options()

  6. # chrome_options.add_argument("--headless")

  7. s = Service(r"chromedriver.exe")

  8. driver = uc.Chrome(service=s, options=chrome_options)

  9. driver.get(url='URL')

  10. # driver.get(url='https://bot.sannysoft.com/')

  11. driver.save_screenshot('result.png')

  12. time.sleep(100)

5. 操作已开启的浏览器

最后一种方式上篇文章已经介绍过

如何利用 Selenium 对已打开的浏览器进行爬虫!

我们只需要通过命令行启动一个浏览器

  1. import subprocess

  2. # 1、打开浏览器

  3. # 指定端口号为:1234

  4. # 配置用户数据路径:--user-data-dir

  5. cmd = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe --remote-debugging-port=1234 --user-data-dir="C:\\selenum\\user_data"'

  6. subprocess.run(cmd)

然后,利用 Selenium 直接操作上面的浏览器即可模拟正常操作浏览器的行为

  1. import time

  2. from selenium import webdriver

  3. from selenium.webdriver.chrome.options import Options

  4. from selenium.webdriver.chrome.service import Service

  5. # 操作上面已经打开的浏览器,进行百度搜索

  6. chrome_options = Options()

  7. # 指定已经打开浏览器的地址及端口号

  8. chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1234")

  9. # 注意:chrome版本与chromedirver驱动要保持一致

  10. # 下载地址:http://chromedriver.storage.googleapis.com/index.html

  11. s = Service(r"chromedriver.exe")

  12. driver = webdriver.Chrome(service=s, options=chrome_options)

  13. # 打开目标网站

  14. driver.get(url="URL")

  15. time.sleep(200)

6. 最后

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

Selenium是一个常用的自动化测试工具,可以模拟人类操作浏览器,包括修改浏览器指纹。下面是使用Selenium修改浏览器指纹的一般步骤: 1. 安装Selenium库:使用pip命令安装Selenium库,例如`pip install selenium`。 2. 下载浏览器驱动:根据你使用的浏览器,下载对应的浏览器驱动。Selenium需要根据浏览器驱动来控制浏览器。常见的浏览器驱动如Chrome驱动(chromedriver)、Firefox驱动(geckodriver)等。 3. 配置浏览器驱动路径:将下载的浏览器驱动放在合适的位置,并将其路径配置到系统环境变量中,或者在代码中指定驱动路径。 4. 创建浏览器实例:使用Selenium创建一个浏览器实例,例如使用Chrome浏览器: ```python from selenium import webdriver # 指定Chrome驱动路径 driver = webdriver.Chrome(executable_path='path/to/chromedriver') ``` 5. 修改浏览器指纹:通过修改浏览器的User-Agent来修改浏览器指纹。以下是一个示例代码: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options # 创建Chrome选项对象 options = Options() # 设置User-Agent options.add_argument('--user-agent="Your User Agent"') # 创建Chrome浏览器实例 driver = webdriver.Chrome(executable_path='path/to/chromedriver', options=options) ``` 在`--user-agent`后面填写你想要设置的User-Agent。 6. 执行自动化操作:使用Selenium的相关方法,如`get()`访问网页,`find_element()`定位元素等,执行你需要的自动化操作。 需要注意的是,使用Selenium修改浏览器指纹可能会触发一些网站的反爬虫机制,因为Selenium模拟的是真实浏览器行为。在使用时,应遵守网站的规则,并进行合法合规的爬取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值