前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python selenium chrome使用验证代理

python selenium chrome使用验证代理

作者头像
py3study
发布2020-01-20 11:39:42
1.3K0
发布2020-01-20 11:39:42
举报
文章被收录于专栏:python3python3

python selenium chrome使用验证代理

代码语言:javascript
复制
#!/usr/bin/env python
# coding: utf-8
import zipfile
import string
from selenium import webdriver
import time


def create_proxyauth_extension(proxy_host, proxy_port,
                               proxy_username, proxy_password,
                               scheme='http', plugin_path=None):

    if plugin_path is None:
        plugin_path = r'D:\link_classification_spider\proxy_auth_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


if __name__ == '__main__':
    proxyauth_plugin_path = create_proxyauth_extension(
        proxy_host="proxy.abuyun.com",
        proxy_port=9020,
        proxy_username="username",
        proxy_password="password",
        plugin_path='proxy_auth_plugin.zip',
    )
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--start_maximized")
    chrome_options.add_extension(proxyauth_plugin_path)
    browser = webdriver.Chrome(chrome_options=chrome_options)
    browser.get('http://httpbin.org/get')
    browser.implicitly_wait(20)
    time.sleep(10)
    browser.quit()

如果当前目录没有proxy_auth_plugin.zip包,会自动创建

运行结果:

image.png
image.png

PS:经测试,在无头模式下 验证代理使用失败,报错信息如下:

failed to wait for extension background page to load: chrome-extension://oonccdcfgindlahlpdgfkafbecgknmkg/_generated_background_page.html

from unknown error: page could not be found: chrome-extension://oonccdcfgindlahlpdgfkafbecgknmkg/_generated_background_page.html

image.png
image.png

不支持加载插件之类的报错信息,不知道是代理 还是 chromedriver的问题,google了很久 没有找到解决方案

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档