-
.NET C# 获取免费代理IP
66ip代理:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Security.Policy; namespace Proxy { public partial class Form1 : Form { private static string StrUrl = "http://www.66ip.cn/mo.php?sxb=&tqsl=10&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=";//实例化timer System.Timers.Timer t = new System.Timers.Timer(); //定义委托变量 public happy updateTxt_ok; public delegate void happy(); public Form1() { InitializeComponent(); //设定timer t.Interval = 100000; //定时,单位ms t.Enabled = true; //回调函数允许执行 t.AutoReset = false; //回调函数循环执行 t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件; t.Stop(); } //timer事件 public void theout(object source, System.Timers.ElapsedEventArgs e) { this.BeginInvoke(updateTxt_ok);//委托执行updateTxt_ok } //委托执行函数 public void UpdateTxtMethod() { try { HtmlDocument html = this.webBrowser1.Document; string IpStr = html.Body.InnerText.Trim().Replace("\r\n", ";"); DBA.SqlDbAccess.ExecNoQuery(CommandType.Text, string.Format("insert into iptable(ip) values('{0}')", IpStr)); } catch (Exception ex) { //记录日志 } webBrowser1.Navigate(StrUrl); webBrowser1_DocumentCompleted(null, null); } private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate(StrUrl); webBrowser1_DocumentCompleted(null, null); } private void Form1_Load(object sender, EventArgs e) { updateTxt_ok = new happy(UpdateTxtMethod); button1_Click(null,null); } private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作 { t.Start();//开启时钟 } } }
89ip代理:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Security.Policy; namespace 数据提取 { public partial class Form1 : Form { private static string StrUrl_66 = "http://www.66ip.cn/nmtq.php?getnum=10&isp=0&anonymoustype=0&start=&ports=&export=&ipaddress=&area=0&proxytype=2&api=66ip"; private static string StrUrl_89 = "http://www.89ip.cn/tqdl.html?api=1&num=30&port=&address=&isp="; private static string StrUrl = StrUrl_89; //实例化timer System.Timers.Timer t = new System.Timers.Timer(); //定义委托变量 public happy updateTxt_ok; public delegate void happy(); public Form1() { InitializeComponent(); //设定timer t.Interval = 100000; //定时,单位ms t.Enabled = true; //回调函数允许执行 t.AutoReset = false; //回调函数循环执行 t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件; t.Stop(); } //timer事件 public void theout(object source, System.Timers.ElapsedEventArgs e) { this.BeginInvoke(updateTxt_ok);//委托执行updateTxt_ok } //委托执行函数 public void UpdateTxtMethod() { try { HtmlDocument html = this.webBrowser1.Document; string IpStr = html.Body.InnerText.Trim().Replace("更好用的代理ip请访问:https://www.7yip.cn/", ""); if (IpStr.LastIndexOf("更好用的代理") > 0) { IpStr = IpStr.Substring(0, IpStr.LastIndexOf("更好用的代理")).Trim().Replace("\r\n", ";"); } DBA.SqlDbAccess.ExecNoQuery(CommandType.Text, string.Format("insert into iptable(ip) values('{0}')", IpStr)); } catch (Exception ex) { //记录日志 } webBrowser1.Navigate(StrUrl); webBrowser1_DocumentCompleted(null, null); } private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate(StrUrl); webBrowser1_DocumentCompleted(null, null); } private void Form1_Load(object sender, EventArgs e) { updateTxt_ok = new happy(UpdateTxtMethod); button1_Click(null,null); } private void webBrowser1_DocumentCompleted(object sender, EventArgs e)//这个就是当网页载入完毕后要进行的操作 { t.Start();//开启时钟 } } }
以翻译接口演示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Text.RegularExpressions; using LibHW; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.IO; using System.Security.Cryptography; namespace KXDataToDB { public class TranslateApi { static List<string> lstIP = new List<string>(); static string ip = null; //读取数据库 private void GetIPList() { string strip = DBA.SqlDbAccess.ExecuteScalar(System.Data.CommandType.Text, "select top 1 ip from iptable order by id desc").ToString(); foreach (string item in strip.Split(';')) { string ip = "http://" + item.Trim(); lstIP.Add(ip); } }private string getnewIP() { if (ip == null) { if (lstIP.Count == 0) { GetIPList(); } ip = lstIP[0].ToString(); lstIP.RemoveAt(0); } return ip; }public string TranslateGoogle(string q, string from, string to) { bool isError = true; string result = string.Empty; while (isError) { try { string url = string.Format( "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids={0}&bl={1}&soc-app={2}&soc-platform={3}&soc-device={4}&rt={5}", "MkEWBc", "boq_translate-webserver_20210927.13_p0", "1", "1", "1", "c"); var from_data = "f.req=" + System.Web.HttpUtility.UrlEncode( string.Format("[[[\"MkEWBc\",\"[[\\\"{0}\\\",\\\"{1}\\\",\\\"{2}\\\",true],[null]]\", null, \"generic\"]]]", ReplaceString(q), from, to), Encoding.UTF8).Replace("+", "%20"); byte[] postData = Encoding.UTF8.GetBytes(from_data); WebClientPro client = new WebClientPro(10000); WebProxy proxy = new WebProxy(); proxy.UseDefaultCredentials = false; proxy.Address = new Uri(getnewIP()); client.Proxy = proxy; client.Encoding = Encoding.UTF8; client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); client.Headers.Add("ContentLength", postData.Length.ToString()); client.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"); byte[] responseData = client.UploadData(url, "POST", postData); string content = Encoding.UTF8.GetString(responseData); result = MatchResult(content); System.Threading.Thread.Sleep(new Random().Next(200, 500)); isError = false; } catch (Exception e) { ip = null; isError = true; } } return result; } /// <summary> /// 匹配翻译结果 /// </summary> /// <returns></returns> public static string MatchResult(string content) { string result = ""; string patttern = @",\[\[\\\""(.*?)\\\"","; Regex regex = new Regex(patttern); MatchCollection matchcollection = regex.Matches(content); if (matchcollection.Count > 0) { List<string> list = new List<string>(); foreach (Match match in matchcollection) { list.Add(match.Groups[1].Value); } result = string.Join(" ", list.Distinct()); if (result.LastIndexOf(@"\""]]]],\""") > 0) { result = result.Substring(0, result.LastIndexOf(@"\""]]]],")); } } return result; } /// <summary> /// 替换部分字符串 /// </summary> /// <param name="sPassed">需要替换的字符串</param> /// <returns></returns> public static string ReplaceString(string JsonString) { if (JsonString == null) { return JsonString; } if (JsonString.Contains("\\")) { JsonString = JsonString.Replace("\\", "\\\\"); } if (JsonString.Contains("\'")) { JsonString = JsonString.Replace("\'", "\\\'"); } if (JsonString.Contains("\"")) { JsonString = JsonString.Replace("\"", "\\\\\\\""); } //去掉字符串的回车换行符 JsonString = Regex.Replace(JsonString, @"[\n\r]", ""); JsonString = JsonString.Trim(); return JsonString; } } }
WebClient重写:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace KXDataToDB { public class WebClientPro : WebClient { /// <summary> /// 过期时间 /// </summary> public int Timeout { get; set; } public WebClientPro(int timeout = 30000) {//默认30秒 Timeout = timeout; } /// <summary> /// 重写GetWebRequest,添加WebRequest对象超时时间 /// </summary> /// <param name="address"></param> /// <returns></returns> protected override WebRequest GetWebRequest(Uri address) {//WebClient里上传下载的方法很多,但最终应该都是调用了这个方法 HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.Timeout = Timeout; request.ReadWriteTimeout = Timeout; return request; } } }
原文:https://www.cnblogs.com/wang920607/p/15378846.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式