首页 > 其他分享 >Selenium 隐藏浏览器指纹特征 转载

Selenium 隐藏浏览器指纹特征 转载

时间:2023-02-01 09:26:02浏览次数:51  
标签:webdriver 浏览器 chrome Selenium driver 指纹 import options selenium

转载自公众号 AirPython 

大家好,我是安果!

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

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

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

1. 直接爬取

目标对象:

aHR0cHM6Ly9xaWthbi5jcXZpcC5jb20vUWlrYW4vU2VhcmNoL0FkdmFuY2U=

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

# 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

https://chromedevtools.github.io/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. 最后

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

标签:webdriver,浏览器,chrome,Selenium,driver,指纹,import,options,selenium
From: https://www.cnblogs.com/testzcy/p/17081401.html

相关文章

  • selenium源码通读·12 |webdriver/remote分析
    (·12|webdriver/remote分析)1源码路径selenium/webdriver/remote2功能说明方法描述说明command.pyDefinesconstantsforthestandardWebDrivercom......
  • 软件测试|Selenium常见api
    Selenium常用APIWebDriver常用API打开浏览器用法:​​driver.get()​​driver=webdriver.Chrome()#打开浏览器driver.get("https://www.baidu.com/")设置浏览器最大化用......
  • “胡歌:我当爸爸啦”冲上热搜榜,这款浏览器果然适合吃瓜
    1月31日,一打开手机就看到胡歌官宣结婚生女冲上了热搜榜。这一消息引来网友的热议,很多网友纷纷送上自己的祝福。当然了,也有的网友十分惊讶,大呼“失恋”了。40岁胡歌官宣已......
  • pyppeteer 下载 chromium 浏览器报错解决方法 (2020.05.31)
    pyppeteer运行需要chromium浏览器,第一次运行时候会自动下chromium浏览器,但是由于网络问题,国内下载会报连接错误解决方法:方法1(推荐):下载chromium浏览器到本地,百度搜......
  • java selenium demo
    importjava.util.Arrays;importjava.util.List;importjava.util.concurrent.TimeUnit;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.C......
  • 盘点保护隐私安全的浏览器,密码锁屏这个功能,真香
    在互联网时代,大家都比较关心自己的隐私安全。一些互联网公司和在线客服会跟踪用户的在线活动,收集用户的个人信息,有时候甚至因为个人的不良习惯导致信息泄露,因此选择隐私和......
  • node webkit使用默认浏览器打开连接
    我们使用nw进行软件开发,有时候需要打开连接,但是正常情况下,nw会默认使用nw打开连接,而我们需要使用默认的浏览器打开,具体流程:首先,引入nw的模块letgui=require('nw.gui');......
  • Webdriver中关闭浏览器的quit和close有什么区别
    简单来说,这两个都可以实现退出浏览器session功能,close是关闭你当前聚焦的tab页面,而quit是关闭全部浏览器tab页面,并退出浏览器session。quit一般用在结束测试之前的操作,  ......
  • 在selenium中如何处理多窗口?
    这个多窗口之间跳转处理,在实际selenium自动化测试经常遇到。点击一个链接,这个链接会在一个新的tab打开,然后接下来要查找元素在新tab打开的页面,需要先将driver切换至window,......
  • 使用selenium.webdriver操作无头模式--haedless浏览器上传文件
    第一种情况Selenium官方文档的方法:文件上传|Seleniumfromseleniumimportwebdriverfromwebdriver_manager.chromeimportChromeDriverManagerdriver=webdriv......