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

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

我们在做爬虫的时候,会遇到一些商业网站对爬虫程序限制较多,在数据采集的过程中对爬虫请求进行了多种验证,导致爬虫程序需要深入分析目标网站的反爬策略,定期更新和维护爬虫程序,增加了研发的时间和投入成本。这种情况下,使用无头浏览器例如 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参数下的文件存放目录是存在的,同时程序拥有该目录的读写权限,否则浏览器会出现代理认证信息读取失败的情况,就会强制弹出认证窗口,要求输入代理用户名和密码,出现程序运行中断的情况。

相关文章
|
1月前
|
数据采集 Web App开发 数据挖掘
利用Python和Selenium实现定时任务爬虫
利用Python和Selenium实现定时任务爬虫
|
1月前
|
数据采集 Web App开发 存储
Selenium库编写爬虫详细案例
Selenium库编写爬虫详细案例
|
3天前
|
前端开发 测试技术 UED
使用Selenium WebDriver模拟用户操作防止滑动条验证
在进行Web自动化测试时,经常会遇到各种前端验证机制,如滑动条验证,这些机制设计用来防止自动化脚本模拟用户行为。在本文中,我们将探讨如何使用Selenium WebDriver来模拟用户操作,以规避这些验证机制。
|
1月前
|
数据采集 Web App开发 搜索推荐
突破目标网站的反爬虫机制:Selenium策略分析
突破目标网站的反爬虫机制:Selenium策略分析
|
26天前
|
数据采集 Web App开发 JavaScript
Selenium与PhantomJS:自动化测试与网页爬虫的完美结合
Selenium与PhantomJS:自动化测试与网页爬虫的完美结合
|
1月前
|
Web App开发 JavaScript Java
《手把手教你》系列技巧篇(二十八)-java+ selenium自动化测试-处理模态对话框弹窗(详解教程)
【4月更文挑战第20天】本文主要介绍了如何使用Selenium处理网页中的alert弹窗,包括accept()、dismiss()、getText()和sendKeys()等方法。文章首先简述了在前一篇文章中提及的switchTo()方法,然后详细讲解了alert弹窗的几个关键方法。接着,作者给出了一个名为ModalDialogueBox.html的测试页面,展示了警告框、确认框和提示框三种类型的模态对话框,并提供了相应的JavaScript代码。最后,文章提供了一个实际的项目实战案例,展示了如何在Java中使用Selenium处理alert弹窗,并给出了相关代码示例。
16 0
|
1月前
|
数据采集 Web App开发 JavaScript
Python 网络爬虫技巧分享:优化 Selenium 滚动加载网易新闻策略
Python 网络爬虫技巧分享:优化 Selenium 滚动加载网易新闻策略
|
1月前
|
数据采集 Web App开发 前端开发
Python爬虫之自动化测试Selenium#7
Selenium基本使用、查找结点、节点交互、动作链、获取节点信息、延时等待、前进后退、Cookies、选项卡管理、异常处理【2月更文挑战第26天】
51 1
Python爬虫之自动化测试Selenium#7
|
1月前
|
数据采集 JavaScript 前端开发
深度剖析Selenium与Scrapy的黄金组合:实现动态网页爬虫
深度剖析Selenium与Scrapy的黄金组合:实现动态网页爬虫
|
1月前
|
Web App开发 数据采集 前端开发
Python Selenium 爬虫淘宝案例
本文基于Selenium + MongoDB + ChromeDriver + Pyquery实现爬虫淘宝案例。【2月更文挑战第11天】
146 1
Python Selenium 爬虫淘宝案例