selenium配置有账号密码验证的代理

selenium配置有账号密码验证的代理[Python基础]

一.准备文件

1.background.js

var config = {

mode: "fixed_servers",

rules: {

singleProxy: {

scheme: "http",

host: "mimvp_proxy_host",

port: parseInt(mimvp_proxy_port)

},

bypassList: ["mimvp.com"]

}

};

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

function callbackFn(details) {

return {

authCredentials: {

username: "mimvp_username",

password: "mimvp_password"

}

};

}

chrome.webRequest.onAuthRequired.addListener(

callbackFn,

{urls: ["<all_urls>"]},

["blocking"]

);

2.manifest.json

var config = {

mode: "fixed_servers",

rules: {

singleProxy: {

scheme: "http",

host: "mimvp_proxy_host",

port: parseInt(mimvp_proxy_port)

},

bypassList: ["mimvp.com"]

}

};

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

function callbackFn(details) {

return {

authCredentials: {

username: "mimvp_username",

password: "mimvp_password"

}

};

}

chrome.webRequest.onAuthRequired.addListener(

callbackFn,

{urls: ["<all_urls>"]},

["blocking"]

);

3.create_proxy_auth_extension.py

import random

import string

import zipfile

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-dyn.dobel.com_9020.zip".format(proxy_username, proxy_password)

manifest_json = """

{

"version": "1.0.0",

"manifest_version": 2,

"name": "Dobel 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

2.生成插件运行selenium

from selenium.webdriver import Chrome

from selenium.webdriver import ChromeOptions

from create_proxy_auth_extension import create_proxy_auth_extension

proxyHost = "ip"

proxyPort = "port"

# 代理隧道验证信息(账号+密码)

proxyUser = "user"

proxyPass = "password"

chrome_options = ChromeOptions()

chrome_options.add_extension(create_proxy_auth_extension(proxyHost, proxyPort, proxyUser, proxyPass))

driver = Chrome(options=chrome_options)

driver.get("http://ipconfig.cc/")

以上是 selenium配置有账号密码验证的代理 的全部内容, 来源链接: utcz.com/z/538104.html

回到顶部