python selenium:如何使用带有登录名和密码的代理



我正在尝试使用Selenium代理。该代理需要登录名和密码。通常以这种格式给出:http://user:password@IP_ADDRESS:port我正在使用chrome,这是我当前的代码:

if config.proxy:
chrome_options.add_argument("--proxy-server=%s" % config.proxy)

不幸的是,这似乎不起作用。有人建议我使用chrome扩展。我用的是linux。你有什么建议?

这是在https://botproxy.net/docs/how-to/setting-chromedriver-proxy-auth-with-selenium-using-python/上找到的解决方案以下是我使用的版本,以防网站不再存在:

import os
import zipfile
from selenium import webdriver

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"
}
"""
def get_background_js(proxy_user, proxy_pass, proxy_host, proxy_port):
return """var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % (proxy_host, proxy_port, proxy_user, proxy_pass)

def get_chromedriver(proxy=False, user_agent=None):
chrome_options = webdriver.ChromeOptions()
if proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", get_background_js(*proxy.split(":")))
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome(
chrome_options=chrome_options)
return driver

只需提供一个格式为user:password:IP_ADDRESS:port的代理。

最新更新