selenium添加认证代理
-
创建一个文件夹:Chrome-proxy-helper
-
在Chrome-proxy-helper文件夹下创建:background.js,内容如下
var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "http", host: "%proxy_host", port: parseInt(%proxy_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'] );
-
在Chrome-proxy-helper文件夹下创建: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_chrome_proxy_extension(proxy): """ 获取一个Chrome代理扩展,里面配置有指定的代理(带用户名密码认证) proxy - 指定的代理,格式: username:password@ip:port """ m = re.compile('([^:]+):([^\@]+)\@([\d\.]+):(\d+)').search(proxy) if m: # 提取代理的各项参数 username = m.groups()[0] password = m.groups()[1] ip = m.groups()[2] port = m.groups()[3] # 创建一个定制Chrome代理扩展(zip文件) if not os.path.exists(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR): os.mkdir(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR) extension_file_path = os.path.join(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR, '{}.zip'.format(proxy.replace(':', '_'))) if not os.path.exists(extension_file_path): # 扩展文件不存在,创建 zf = zipfile.ZipFile(extension_file_path, mode='w') zf.write(os.path.join(CHROME_PROXY_HELPER_DIR, 'manifest.json'), 'manifest.json') # 替换模板中的代理参数 background_content = open(os.path.join(CHROME_PROXY_HELPER_DIR, 'background.js')).read() background_content = background_content.replace('%proxy_host', ip) background_content = background_content.replace('%proxy_port', port) background_content = background_content.replace('%username', username) background_content = background_content.replace('%password', password) zf.writestr('background.js', background_content) zf.close() return extension_file_path else: raise Exception('Invalid proxy format. Should be username:password@ip:port')
-
创建webdriver对象添加插件:
options = webdriver.ChromeOptions() # 添加一个自定义的代理插件(配置特定的代理,含用户名密码认证) options.add_extension(get_chrome_proxy_extension(proxy="username:password@ip:port")) # username:password@ip:port driver = webdriver.Chrome(options=options)