环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit)

Selenium官方给的Firefox代理配置方式并不起效,也没看到合适的配置方式,对于Chrome Selenium官方没有告知如何配置,但以下两种方式是有效的:

1. 连接无用户名密码认证的代理

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=http://ip:port')
driver = webdriver.Chrome(chrome_options=chromeOptions)

2. 有用户名和密码的连接

from selenium import webdriverdef create_proxyauth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
"""Proxy Auth Extension args:
proxy_host (str): domain or ip address, ie proxy.domain.com
proxy_port (int): port
proxy_username (str): auth username
proxy_password (str): auth password
kwargs:
scheme (str): proxy scheme, default http
plugin_path (str): absolute path of the extension return str -> plugin_path
"""
import string
import zipfile if plugin_path is None:
plugin_path = 'd:/webdriver/vimm_chrome_proxyauth_plugin.zip' manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"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 proxyauth_plugin_path = create_proxyauth_extension(
proxy_host="proxy.crawlera.com",
proxy_port=8010,
proxy_username="fea687a8b2d448d5a5925ef1dca2ebe9",
proxy_password=""
) co = webdriver.ChromeOptions()
co.add_argument("--start-maximized")
co.add_extension(proxyauth_plugin_path) driver = webdriver.Chrome(chrome_options=co)
driver.get("http://www.amazon.com/")

以上直接通过python代码生成chrome所需的zip插件文件,IP端口用户名密码写上自己的,原文出处:

https://vimmaniac.com/blog/bangal/selenium-chrome-driver-proxy-with-authentication/

插件源代码 https://github.com/RobinDev/Selenium-Chrome-HTTP-Private-Proxy

Selenium chrome配置代理Python版的更多相关文章

  1. python爬虫——selenium+chrome使用代理

    先看下本文中的知识点: python selenium库安装 chrome webdirver的下载安装 selenium+chrome使用代理 进阶学习 搭建开发环境: selenium库 chro ...

  2. Selenium chrome配置不加载图片Python版

    环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit) f ...

  3. Selenium启动Firefox示例(python版)

    目前做selenium自动化使用的主流语言分为java和python,前一篇为java版,本篇介绍python实现selenium启动Firefox. 1 #-*- coding:utf-8 -*- ...

  4. selenium 速查手册 python版

    1.安装与配置 pip install selenium 基本使用selenium都是为了动态加载网页内容用于爬虫,所以一般也会用到phantomjs mac下如果要配置phantomjs环境的话 e ...

  5. Vim配置(python版)

    由于马上将用到django框架,需要有一个好的ide来coding,之前做C的开发时候体会到了vim的强大,所以编写python也决定采用vim. PS:除了vim,一般浏览代码多用atom和subl ...

  6. Selenium chrome配置不加载图片

    from selenium import webdriver chrome_options = webdriver.ChromeOptions() prefs = {"profile.man ...

  7. 芝麻HTTP:设置Selenium+Chrome代理

    微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...

  8. 芝麻HTTP:爬虫之设置Selenium+Chrome代理

    微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...

  9. 小白学爬虫-设置Selenium+Chrome代理

    微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...

随机推荐

  1. 重庆/北京/江苏KS/快乐时时/七星/福运来菠菜电商开奖修复APP网站SSC网站程序开发php

    网站制作是指使用标识语言(markup language),通过一系列设计.建模.和执行的过程将电子格式的信息通过互联网传输,最终以图形用户界面(GUI)的形式被用户所浏览.简单来说,网页设计的目的就 ...

  2. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  3. 清理我的 Mac

    在Macbook使用久之后,会发现本来还富裕的硬盘,变得越来越少,尤其现在Macbook使用容量很小的固态硬盘.在此种情况下,该如何清理Macbook垃圾文件,以保证Macbook有足够空间做其他事情 ...

  4. NancyFx 2.0的开源框架的使用-AspnetBootstrapping

    新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...

  5. React-Router 4 的新玩意儿

    上一个项目用的还是 2.6.1,转眼的功夫 4.0 都发布了,API 变化实在有点大,2.X那套东西不顶用了,老老实实重新看一遍文档,其中有几点需要注意的,拿出来说一说. 本文只讨论针对浏览器的应用, ...

  6. 「CODVES 1922 」骑士共存问题(二分图的最大独立集|网络流)&dinic

    首先是题目链接  http://codevs.cn/problem/1922/ 结果发现题目没图(心情复杂 然后去网上扒了一张图 大概就是这样了. 如果把每个点和它可以攻击的点连一条边,那问题就变成了 ...

  7. Cassandra Issue with Tombstone

    1. Cassandra is quicker than postgre and have lower change to lose data. Cassandra doesn't have fore ...

  8. 局域网内部署 Docker Registry

    在局域网内部署 Docker Registry 可以极大的提升平时 pull.push 镜像的速度,从而缩短自动化操作的过程.同时也可以缓解带宽不足的问题,真是一举多得.本文将从创建单机的 Docke ...

  9. 2D游戏开发(1)

    <外星人入侵> 通过pygame,入门python的2D游戏开发过程,通过教材制作一个打外星人游戏. 1.函数 run_game() import sys import pygame de ...

  10. cas4.2.7 取消https

    cas.properties 修改两个地方 # Decides whether SSO cookie should be created only under secure connections. ...