【问题标题】:Seting up proxies in selenium在 selenium 中设置代理
【发布时间】:2021-03-03 14:19:25
【问题描述】:

我需要在 selenium 中设置 ip:port:user:pass 代理,以便在脚本执行 10 次后自动刷新。我该怎么做?我正在使用 Python 硒

var config = {
    mode: "fixed_servers",
    rules: {
        singleProxy: {
            scheme: "http",
            host: "X",
            port: parseInt(X)
        },
        bypassList: ["foobar.com"]
    }
};

chrome.proxy.settings.set({ value: config, scope: "regular" }, function () { });

function callbackFn(details) {
    return {
        authCredentials: {
            username: "X",
            password: "X"
        }
    };
}

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    为什么不动态生成 `background.js 文件?

    
    import zipfile
    
    
    PROXY_HOSTS = ['proxy1', 'proxy2']
    PROXY_PORT = 8080
    PROXY_USER = ['user1', 'user2'] 
    PROXY_PASS = ['password1', 'password2']
    
    backgroundjs = """
    var config = {
            mode: "fixed_servers",
            rules: {
              singleProxy: {
                scheme: "http",
                host: "{0}",
                port: parseInt({1})
              },
              bypassList: ["foobar.com"]
            }
          };
    
    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    
    function callbackFn(details) {
        return {
            authCredentials: {
                username: "{2}",
                password: "{3}"
            }
        };
    }
    """.format(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
    

    然后:

    用你的变量创建 zip:

    myproxies = zip(PROXY_HOSTS, PROXY_USER, PROXY_PASS)
    

    你会得到类似的东西:

    [('proxy1', 'user1', 'password1'), ('proxy2', 'user2', 'password2')]
    

    迭代并保存:

    with zipfile.ZipFile(pluginfile, 'w') as selzip:
        selzip.writestr("background.js", backgroundjs)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      相关资源
      最近更新 更多