(四)Selenium通过谷歌插件实现使用隧道代理
关于如何使用代理,在快代理或其他代理网站都给出了很好的说明和示例代码,这里说一下如何使用Selenium用隧道代理。Selenium本身是不支持的,因为它的本意是支持自动化测试而不是去爬爬爬(网上看的)。所以这里通过使用谷歌浏览器插件来实现。
1.新建文件夹proxy,插件编写
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"
}
background.js
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "xxx.kdlapi.com",
port: parseInt(xxxx)
},
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "xxxx",
password: "xxxxxxx"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
2.压缩文件夹,proxy.zip
3.加载插件
err=chromeCaps.AddExtension("other/proxy.zip")
参考资料
谷歌插件
[1] https://developer.chrome.com/docs/extensions/reference/ “API查阅指南”