美文网首页
Selenium爬虫过程中遇到弹窗验证

Selenium爬虫过程中遇到弹窗验证

作者: SunY7 | 来源:发表于2023-04-27 15:51 被阅读0次

我们在做爬虫的时候,会遇到一些商业网站对爬虫程序限制较多,在数据采集的过程中对爬虫请求进行了多种验证,导致爬虫程序需要深入分析目标网站的反爬策略,定期更新和维护爬虫程序,增加了研发的时间和投入成本。这种情况下,使用无头浏览器例如 Selenium,模拟用户的请求进行数据采集是更加方便快捷的方式。同时为了避免目标网站出现IP限制,配合爬虫代理,实现每次请求自动切换IP,能够保证长期稳定的数据采集。以python的demo为例:
from selenium import webdriver
import string
import zipfile

代理服务器(产品官网 )

proxyHost = "t.16yun.cn"
proxyPort = "31111"

代理验证信息

proxyUser = "username"
proxyPass = "password"
def create_proxy_auth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
if plugin_path is None:
plugin_path = r'D:/{}_{}@t.16yun.zip'.format(proxy_username, proxy_password)
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "16YUN Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "{scheme}", host: "{host}",
port: parseInt({port}) }, bypassList: ["foobar.com"] } }; chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); function callbackFn(details) { return { authCredentials: { username: "{username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: [""]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
proxy_auth_plugin_path = create_proxy_auth_extension(
proxy_host=proxyHost,
proxy_port=proxyPort,
proxy_username=proxyUser,
proxy_password=proxyPass)
option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")

如报错 chrome-extensions

option.add_argument("--disable-extensions")

option.add_extension(proxy_auth_plugin_path)

关闭webdriver的一些标志

option.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options=option)

修改webdriver get属性

script = '''

Object.defineProperty(navigator, 'webdriver', {

get: () => undefined

})

'''

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})

driver.get(")
要注意必须保证 plugin_path参数下的文件存放目录是存在的,同时程序拥有该目录的读写权限,否则浏览器会出现代理认证信息读取失败的情况,就会强制弹出认证窗口,要求输入代理用户名和密码,出现程序运行中断的情况。

相关文章

  • selenium文件自动保存

      使用selenium进行爬虫开发时,会遇到下载文件时弹窗显示保存还是打开的问题,这时候可以通过代码设置自动下载...

  • 将selenium获得的cookies传递给requests.s

    写爬虫的过程中进行模拟登录的时候,有的网站会有滑块验证,不是很好处理,想到了使用selenium配合request...

  • selenium—js插件元素定位方法

    主题:解决selenium UI自动测试过程中遇到弹窗元素定位不到的问题 前言:我们在做UI自动化测试的时候经常会...

  • selenium 3.8.0 入门第一课

    背景:在爬虫过程中,关键一课就是selenium获取页面信息;selenium 以其优越的性能和封装,可覆盖大部分...

  • 爬虫如何解决图片验证码问题

    第一步安装 Tesserocr的安装爬虫过程中难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们...

  • tesserocr的安装

    在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们可以直接用OCR来识别。 1. ...

  • Python第三方模块tesserocr安装

    在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们可以直接用 OCR 来识别。 t...

  • selenium+python破解滑动验证码

    最近在参与公司大数据项目的测试,其中部分数据来源于网络爬虫,想用selenium辅助测试,无奈有验证码(滑动验证码...

  • selenium绕过爬虫特征检测

    Python + selenium 如何绕过爬虫特征检测? selenium绕过爬虫特征检测_服务器 在信息时代,...

  • appium 弹窗处理

    测试过程中遇到两类弹窗: 系统权限弹窗 具体业务弹窗 系统权限弹窗 Android 系统权限弹窗一般出现在安装 a...

网友评论

      本文标题:Selenium爬虫过程中遇到弹窗验证

      本文链接:https://www.haomeiwen.com/subject/lntijdtx.html