通常,我们给selenium设置代理的方式很简单,网上也有很多示例。
但是我们如何给selenium设置有账号密码验证的代理呢,在Selenium的SDK中并没有这个入口,也没有集成这几个参数。
于是,我查找了网上的一些资料,发现python的案例是很多的,C#没有😶没有。。(好好的C#非得让做点爬虫的事情😂)为了完成任务,我只能是想办法转译成C#代码。分享出来,希望可以帮到同样处境的.Net同僚们。
转译后的代码如下:
这是核心代码,其中的plugin_path是生成的文件路径,当然大家可以自己换路径。其他的代码不用动。
public string create_proxy_auth_extension(string proxy_host,string proxy_port,string proxy_username,string proxy_password,string scheme= "http")
{
string plugin_path = Environment.CurrentDirectory+ "/SelectniumProxy/Proxy";
if (!Directory.Exists(plugin_path))
{
Directory.CreateDirectory(plugin_path);
}
string filePath = plugin_path + $"/{proxy_username}_{proxy_password}@{proxy_host}_{proxy_port}.zip";
string 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\"}";
string background_js ="var config ={mode: \"fixed_servers\",rules:{singleProxy:{scheme: \""+scheme+"\", 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: \""+proxy_username+"\",password: \""+proxy_password+"\"}};}chrome.webRequest.onAuthRequired.addListener(callbackFn,{urls: [\"<all_urls>\"]},['blocking']); ";
using (FileStream zipToOpen = new FileStream(filePath, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry("manifest.json");
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine(manifest_json);
}
ZipArchiveEntry readmeEntry1 = archive.CreateEntry("background.js");
using (StreamWriter writer = new StreamWriter(readmeEntry1.Open()))
{
writer.WriteLine(background_js);
}
}
}
return filePath;
}
下边是调用,host就是你的代理ip,port端口,userName账号,password密码
ChromeOptions options = new ChromeOptions();
options.AddExtension(create_proxy_auth_extension(host, port, userName, password));
祝各位大佬码图坦荡,前程似锦。如有问题,请加以指正~