https://registry.npmmirror.com/binary.html?path=chromedriver/
以下方式均实机测试过。
无需验证,只需填写代理IP和端口号
from selenium import webdriver
chrome_driver = "F:\PycharmProjects\chromedriver_win32\chromedriver.exe"
option = webdriver.ChromeOptions()
option.add_argument('--proxy-server=http://代理IP:端口号')
driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=option)
driver.get("https://proxy.mimvp.com/ip.php/")
driver.close()
通过生成插件并加载通过代理验证,需要填写代理IP、端口号、验证账号和密码,该方式缺点是不支持后台静默运行(headless)
from selenium import webdriver
import zipfile
import string
chrome_driver = "F:\PycharmProjects\chromedriver_win32\chromedriver.exe"
# 生成代理插件,返回插件路径
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'{}_{}@h.test.com.zip'.format(proxy_username, proxy_password)
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"",
"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: ${port}
},
bypassList: ["127.0.0.1"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{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__':
# 插件路径
proxy_auth_plugin_path = create_proxy_auth_extension(
proxy_host="代理IP",
proxy_port="代理端口",
proxy_username="验证账号",
proxy_password="验证密码")
option = webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-logging"])
option.page_load_strategy = "eager"
option.add_argument('ignore-certificate-errors')
option.add_extension(proxy_auth_plugin_path)
# option.add_argument('headless') # 该模式不支持后台静默运行
driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=option)
driver.get("https://proxy.mimvp.com/ip.php/") # 查看是否代理的ip
driver.close()