C# selenium设置具有账号密码的代理IP

通常,我们给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));

祝各位大佬码图坦荡,前程似锦。如有问题,请加以指正~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值