美文网首页
chromedriver 使用待认证的动态代理--以阿布云为例(

chromedriver 使用待认证的动态代理--以阿布云为例(

作者: 绝世一只猫 | 来源:发表于2018-12-15 13:16 被阅读0次
  • 以下为我在阿布云上提交的代码示例
    代码中打包一个json和一段JavaScript为zip压缩包
    然后通过 option.add_extension 命令安装至chrome 通过插件实现动态代理
from selenium import webdriver
import string
import zipfile

# 代理服务器
proxyHost = "http-pro.abuyun.com"
proxyPort = "9010"

# 代理隧道验证信息
proxyUser = "H01234567890123P"
proxyPass = "0123456789012345"


def create_proxy_auth_extension(proxy_host, proxy_port,
                                proxy_username, proxy_password,
                                scheme='http', plugin_path=None):
    if plugin_path is None:
        plugin_path = r'./{}_{}@http-pro.abuyun.com_9010.zip'.format(proxy_username, proxy_password)

    manifest_json = """
        {
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Abuyun 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


proxy_auth_plugin_path = create_proxy_auth_extension(
    proxy_host=proxyHost,
    proxy_port=proxyPort,
    proxy_username=proxyUser,
    proxy_password=proxyPass)

option = webdriver.ChromeOptions()

option.add_argument("--start-maximized")
option.add_extension(proxy_auth_plugin_path)

driver = webdriver.Chrome(chrome_options=option)

driver.get("http://test.abuyun.com")

装插件后无法直接使用无界面模式运行,需要通过虚拟现实技术间接实现,详情参考: chromedriver使用插件后如何在无界面headless环境下运行

相关文章

  • chromedriver 使用待认证的动态代理--以阿布云为例(

    以下为我在阿布云上提交的代码示例代码中打包一个json和一段JavaScript为zip压缩包然后通过 optio...

  • scrapy代理ip之使用动态转发IP设置

    以阿布云提供的ip动态转发为例,在python3中接入随机动态ip只需要在middleware中添加如下代码: 在...

  • chromedriver 使用插件后如何在无界面环境下运行

    解决一个初学者使用chromedriver添加认证代理时不能使用headless的问题此方案只适用于Linux系统...

  • JAVA 动态代理

    代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问。 动态代理的使用 动态代理类都必须要实现Invo...

  • 2020-06-08

    网易云课堂-动态代理和静态代理 代理模式:代理模式为其他对象提供了一种代理,以控制对这个对象的访问。代理对象在客户...

  • NVIDIA NGC镜像国内代理

    阿里云貌似有部分NGC镜像的国内代理。以Pytorch为例,NGC官网的上的镜像为: tag 列表为: 在阿里云上...

  • Python接口自动化笔记1

    Fidder-->抓包工具 (1)使用Fidder时先设置代理以火狐为例(火狐默认不更改代理设置)设置如下:工具-...

  • 代理模式

    以追女孩为例,需要人帮忙送礼物 实现 女孩 接口 男孩 代理同学 使用 打印

  • Java 代理

    静态代理 动态代理 动态代理, 日志切片使用反射获得方法 动态代理, 自定义注解(对注解的方法,使用动态代理添加切...

  • Java实现简单的RPC框架

    动态代理例子 使用动态代理的步骤很简单, 可以概括为如下两步: 实现 InvocationHandler 接口, ...

网友评论

      本文标题:chromedriver 使用待认证的动态代理--以阿布云为例(

      本文链接:https://www.haomeiwen.com/subject/fksihqtx.html