微博登录限制了错误次数···加上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 这是个好地方啊)

走你!

  1. # -*- coding: utf- -*-
  2. # @Time : // :
  3. # @Author : 哎哟卧槽
  4. # @Site :
  5. # @File : pubilc.py
  6. # @Software: PyCharm
  7.  
  8. import string
  9. import zipfile
  10.  
  11. def create_proxyauth_extension(proxy_host, proxy_port,
  12. proxy_username, proxy_password,
  13. scheme='http', plugin_path=None):
  14. """代理认证插件
  15.  
  16. args:
  17. proxy_host (str): 你的代理地址或者域名(str类型)
  18. proxy_port (int): 代理端口号(int类型)
  19. proxy_username (str):用户名(字符串)
  20. proxy_password (str): 密码 (字符串)
  21. kwargs:
  22. scheme (str): 代理方式 默认http
  23. plugin_path (str): 扩展的绝对路径
  24.  
  25. return str -> plugin_path
  26. """
  27.  
  28. if plugin_path is None:
  29. plugin_path = 'vimm_chrome_proxyauth_plugin.zip'
  30.  
  31. manifest_json = """
  32. {
  33. "version": "1.0.0",
  34. ,
  35. "name": "Chrome Proxy",
  36. "permissions": [
  37. "proxy",
  38. "tabs",
  39. "unlimitedStorage",
  40. "storage",
  41. "<all_urls>",
  42. "webRequest",
  43. "webRequestBlocking"
  44. ],
  45. "background": {
  46. "scripts": ["background.js"]
  47. },
  48. "minimum_chrome_version":"22.0.0"
  49. }
  50. """
  51.  
  52. background_js = string.Template(
  53. """
  54. var config = {
  55. mode: "fixed_servers",
  56. rules: {
  57. singleProxy: {
  58. scheme: "${scheme}",
  59. host: "${host}",
  60. port: parseInt(${port})
  61. },
  62. bypassList: ["foobar.com"]
  63. }
  64. };
  65.  
  66. chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
  67.  
  68. function callbackFn(details) {
  69. return {
  70. authCredentials: {
  71. username: "${username}",
  72. password: "${password}"
  73. }
  74. };
  75. }
  76.  
  77. chrome.webRequest.onAuthRequired.addListener(
  78. callbackFn,
  79. {urls: ["<all_urls>"]},
  80. ['blocking']
  81. );
  82. """
  83. ).substitute(
  84. host=proxy_host,
  85. port=proxy_port,
  86. username=proxy_username,
  87. password=proxy_password,
  88. scheme=scheme,
  89. )
  90. with zipfile.ZipFile(plugin_path, 'w') as zp:
  91. zp.writestr("manifest.json", manifest_json)
  92. zp.writestr("background.js", background_js)
  93.  
  94. return plugin_path
  95.  

使用方法:

  1.  
  2. from selenium import webdriver
  3. from common.pubilc import create_proxyauth_extension
  4.  
  5. proxyauth_plugin_path = create_proxyauth_extension(
  6. proxy_host="XXXXX.com",
  7. proxy_port=,
  8. proxy_username="XXXXXXX",
  9. proxy_password="XXXXXXX"
  10. )
  11.  
  12. co = webdriver.ChromeOptions()
  13. # co.add_argument("--start-maximized")
  14. co.add_extension(proxyauth_plugin_path)
  15.  
  16. driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", chrome_options=co)
  17. driver.get("http://ip138.com/")
  18. print(driver.page_source)
  19.  

无认证代理:

  1. options = webdriver.ChromeOptions()
  2. options.add_argument('--proxy-server=http://ip:port')
  3. driver = webdriver.Chrome(executable_path="C:\chromedriver.exe", chrome_options=0ptions)
  4. driver.get("http://ip138.com/")
  5. print(driver.page_source)

以上完毕 So Easy

芝麻HTTP:爬虫之设置Selenium+Chrome代理的更多相关文章

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

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

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

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

  3. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  4. charles 设置为chrome代理

    本文参考:charles 设置为chrome代理 将charles设置为chrome的代理 需要注意的是,Chrome 和 Firefox 浏览器并不一定使用的就是本机,可能是一些代理工具,而 Cha ...

  5. Python爬虫教程-28-Selenium 操纵 Chrome

    我觉得本篇是很有意思的,闲着没事来看看! Python爬虫教程-28-Selenium 操纵 Chrome PhantomJS 幽灵浏览器,无界面浏览器,不渲染页面.Selenium + Phanto ...

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

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

  7. 小白学爬虫-在无GUI的CentOS上使用Selenium+Chrome

    爬虫代理IP由芝麻HTTP服务供应商提供各位小伙伴儿的采集日常是不是被JavaScript的各种点击事件折腾的欲仙欲死啊?好不容易找到个Selenium+Chrome可以解决问题! 但是另一个▄█▀█ ...

  8. Selenium Chrome浏览器的启动以及proxy设置

    Selenium Chrome浏览器的启动以及proxy设置   虽然WebDriver对Firefox的支持最好,之前写的脚本也都在Firefox浏览器运行,但最近项目做了整合,发现新整合的功能不太 ...

  9. chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式

    WebDriverException: Message: unknown error: Chrome failed to start: crashed 第一种:如果出现下面情况: chrome浏览器有 ...

随机推荐

  1. ABP官方文档翻译 7.2 Hangfire集成

    Hangfire集成 介绍 ASP.NET Core集成 ASP.NET MVC 5.x集成 面板授权 介绍 Hangfire是一个综合的后台job管理器.你可以 把它集成到ABP,用来取代默认的后台 ...

  2. POJ1743 Musical Theme [后缀自动机]

    题意:不重叠最长重复子串 后缀数组做法:http://www.cnblogs.com/candy99/p/6227659.html 后缀自动机的话,首先|Right|>=2 然后min(t[u] ...

  3. SDP(5):ScalikeJDBC- JDBC-Engine:Streaming

    作为一种通用的数据库编程引擎,用Streaming来应对海量数据的处理是必备功能.同样,我们还是通过一种Context传递产生流的要求.因为StreamingContext比较简单,而且还涉及到数据抽 ...

  4. cnblogs的使用

    cnblogs的使用 选择使用cnblogs而不是csdn,答案是很明显的.csdn每次创建博客之后会有一段时间的审核期,这大大的影响了用户体验.此外,cnblogs的用户群以及使用模式有着很大的诱惑 ...

  5. 【HTTP协议】---TCP三次握手和四次挥手

    TCP三次握手和四次挥手 首先我们知道HTTP协议通常承载于TCP协议之上,HTTPS承载于TLS或SSL协议层之上 通过上面这张图我们能够知道.     在Http工作之前,Web浏览器通过网络和W ...

  6. PHP 支持加解密的函数

    function encrypt($string,$operation,$key=''){ $key=md5($key); $key_length=strlen($key); $string=$ope ...

  7. 理解Activity.runOnUiThread()

    这是一篇译文(中英对照),原文链接:Understanding Activity.runOnUiThread() When developing Android applications we alw ...

  8. PAT1118. Birds in Forest (并查集)

    思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...

  9. uva12563

    一个简单的0-1背包,背包容量为t-1,每个物品价值为1,代价为t[i].背包容量为t-1而不是t的原因是留1s唱<劲歌金曲>. AC代码: #include<cstdio> ...

  10. nyoj 取石子(七) 环形博弈

    手推前几个可以知道规律:n>2时是P态,n<=2时是N态. 注意:石子拿去后,剩下的石子是分散的. AC代码 #include <cstdio> #include <cm ...