芝麻HTTP:爬虫之设置Selenium+Chrome代理
微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分钟解决(那么问题来了?百度除了卖假药还会干啥?)
Selenium+Chrome认证代理不能通过options处理。只能换个方法使用扩展解决
原文地址:https://stackoverflow.com/questions/29983106/how-can-i-set-proxy-with-authentication-in-selenium-chrome-web-driver-using-pyth#answer-30953780 (Stack Overflow 这是个好地方啊)
走你!
-
- # -*- coding: utf- -*-
- # @Time : // :
- # @Author : 哎哟卧槽
- # @Site :
- # @File : pubilc.py
- # @Software: PyCharm
- import string
- import zipfile
- def create_proxyauth_extension(proxy_host, proxy_port,
- proxy_username, proxy_password,
- scheme='http', plugin_path=None):
- """代理认证插件
- args:
- proxy_host (str): 你的代理地址或者域名(str类型)
- proxy_port (int): 代理端口号(int类型)
- proxy_username (str):用户名(字符串)
- proxy_password (str): 密码 (字符串)
- kwargs:
- scheme (str): 代理方式 默认http
- plugin_path (str): 扩展的绝对路径
- return str -> plugin_path
- """
- if plugin_path is None:
- plugin_path = 'vimm_chrome_proxyauth_plugin.zip'
- manifest_json = """
- {
- "version": "1.0.0",
- ,
- "name": "Chrome Proxy",
- "permissions": [
- "proxy",
- "tabs",
- "unlimitedStorage",
- "storage",
- "<all_urls>",
- "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: ["<all_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
-
使用方法:
-
- from selenium import webdriver
- from common.pubilc import create_proxyauth_extension
- proxyauth_plugin_path = create_proxyauth_extension(
- proxy_host="XXXXX.com",
- proxy_port=,
- proxy_username="XXXXXXX",
- proxy_password="XXXXXXX"
- )
- co = webdriver.ChromeOptions()
- # co.add_argument("--start-maximized")
- co.add_extension(proxyauth_plugin_path)
- driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", chrome_options=co)
- driver.get("http://ip138.com/")
- print(driver.page_source)
-
无认证代理:
- options = webdriver.ChromeOptions()
- options.add_argument('--proxy-server=http://ip:port')
- driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", chrome_options=0ptions)
- driver.get("http://ip138.com/")
- print(driver.page_source)
以上完毕 So Easy
芝麻HTTP:爬虫之设置Selenium+Chrome代理的更多相关文章
- 芝麻HTTP:设置Selenium+Chrome代理
微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...
- 小白学爬虫-设置Selenium+Chrome代理
微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...
- Python爬虫之设置selenium webdriver等待
Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...
- charles 设置为chrome代理
本文参考:charles 设置为chrome代理 将charles设置为chrome的代理 需要注意的是,Chrome 和 Firefox 浏览器并不一定使用的就是本机,可能是一些代理工具,而 Cha ...
- Python爬虫教程-28-Selenium 操纵 Chrome
我觉得本篇是很有意思的,闲着没事来看看! Python爬虫教程-28-Selenium 操纵 Chrome PhantomJS 幽灵浏览器,无界面浏览器,不渲染页面.Selenium + Phanto ...
- python爬虫——selenium+chrome使用代理
先看下本文中的知识点: python selenium库安装 chrome webdirver的下载安装 selenium+chrome使用代理 进阶学习 搭建开发环境: selenium库 chro ...
- 小白学爬虫-在无GUI的CentOS上使用Selenium+Chrome
爬虫代理IP由芝麻HTTP服务供应商提供各位小伙伴儿的采集日常是不是被JavaScript的各种点击事件折腾的欲仙欲死啊?好不容易找到个Selenium+Chrome可以解决问题! 但是另一个▄█▀█ ...
- Selenium Chrome浏览器的启动以及proxy设置
Selenium Chrome浏览器的启动以及proxy设置 虽然WebDriver对Firefox的支持最好,之前写的脚本也都在Firefox浏览器运行,但最近项目做了整合,发现新整合的功能不太 ...
- chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式
WebDriverException: Message: unknown error: Chrome failed to start: crashed 第一种:如果出现下面情况: chrome浏览器有 ...
随机推荐
- ABP官方文档翻译 7.2 Hangfire集成
Hangfire集成 介绍 ASP.NET Core集成 ASP.NET MVC 5.x集成 面板授权 介绍 Hangfire是一个综合的后台job管理器.你可以 把它集成到ABP,用来取代默认的后台 ...
- POJ1743 Musical Theme [后缀自动机]
题意:不重叠最长重复子串 后缀数组做法:http://www.cnblogs.com/candy99/p/6227659.html 后缀自动机的话,首先|Right|>=2 然后min(t[u] ...
- SDP(5):ScalikeJDBC- JDBC-Engine:Streaming
作为一种通用的数据库编程引擎,用Streaming来应对海量数据的处理是必备功能.同样,我们还是通过一种Context传递产生流的要求.因为StreamingContext比较简单,而且还涉及到数据抽 ...
- cnblogs的使用
cnblogs的使用 选择使用cnblogs而不是csdn,答案是很明显的.csdn每次创建博客之后会有一段时间的审核期,这大大的影响了用户体验.此外,cnblogs的用户群以及使用模式有着很大的诱惑 ...
- 【HTTP协议】---TCP三次握手和四次挥手
TCP三次握手和四次挥手 首先我们知道HTTP协议通常承载于TCP协议之上,HTTPS承载于TLS或SSL协议层之上 通过上面这张图我们能够知道. 在Http工作之前,Web浏览器通过网络和W ...
- PHP 支持加解密的函数
function encrypt($string,$operation,$key=''){ $key=md5($key); $key_length=strlen($key); $string=$ope ...
- 理解Activity.runOnUiThread()
这是一篇译文(中英对照),原文链接:Understanding Activity.runOnUiThread() When developing Android applications we alw ...
- PAT1118. Birds in Forest (并查集)
思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...
- uva12563
一个简单的0-1背包,背包容量为t-1,每个物品价值为1,代价为t[i].背包容量为t-1而不是t的原因是留1s唱<劲歌金曲>. AC代码: #include<cstdio> ...
- nyoj 取石子(七) 环形博弈
手推前几个可以知道规律:n>2时是P态,n<=2时是N态. 注意:石子拿去后,剩下的石子是分散的. AC代码 #include <cstdio> #include <cm ...