python selenium chrome使用验证代理

发布时间:2019-04-13 10:53:54编辑:Run阅读(5211)

    python selenium chrome使用验证代理

    #!/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


    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

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


关键字