c# 代理IP获取通用方法

时间:2023-03-09 00:19:43
c#  代理IP获取通用方法

调用:

       ConcurrentQueue<string> proxyIpQueue = new ConcurrentQueue<string>();
Grab_ProxyIp(proxyIpQueue);

返回结果:

c#  代理IP获取通用方法

一下是获取代理IP集合的方法:

        #region 生产IP 代理 对象
private void Grab_ProxyIp(ConcurrentQueue<string> proxyIpQueue)
{
HashSet<string> proxyIp = new HashSet<string>(); HttpHelper http = new HttpHelper();
HttpItem para = new HttpItem();
para.Timeout = * ;
para.Method = "GET"; int count = ;
para.URL = "http://www.xicidaili.com/nn/1"; // 西刺
RetryFunc(() =>
{
HttpResult result = http.GetHtml(para);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string regex = @"<td>(\d+\.\d+\.\d+\.\d+)</td>\s+<td>(\d+)</td>";
Match mstr = Regex.Match(result.Html, regex);
while (mstr.Success && count < )
{
proxyIp.Add(mstr.Groups[].Value + ":" + mstr.Groups[].Value);
mstr = mstr.NextMatch();
count++;
} return true;
}
else
{
return false;
}
}, ); count = ;
para.URL = "http://ip84.com/dlgn"; // IP巴士
RetryFunc(() =>
{
HttpResult result = http.GetHtml(para);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string regex = @"<td>(\d+\.\d+\.\d+\.\d+)</td>\s+<td>(\d+)</td>";
Match mstr = Regex.Match(result.Html, regex);
while (mstr.Success && count < )
{
proxyIp.Add(mstr.Groups[].Value + ":" + mstr.Groups[].Value);
mstr = mstr.NextMatch();
count++;
} return true;
}
else
{
return false;
}
}, ); count = ;
para.URL = "http://www.ip3366.net/free/?stype=1"; // 云代理
RetryFunc(() =>
{
HttpResult result = http.GetHtml(para);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string regex = @"<td>(\d+\.\d+\.\d+\.\d+)</td>\s+<td>(\d+)</td>";
Match mstr = Regex.Match(result.Html, regex);
while (mstr.Success && count < )
{
proxyIp.Add(mstr.Groups[].Value + ":" + mstr.Groups[].Value);
mstr = mstr.NextMatch();
count++;
} return true;
}
else
{
return false;
}
}, ); count = ;
para.URL = "http://www.iphai.com/free/ng"; // IP海
RetryFunc(() =>
{
HttpResult result = http.GetHtml(para);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string regex = @"<td>\s+(\d+\.\d+\.\d+\.\d+)\s+</td>\s+<td>\s+(\d+)\s+</td>";
Match mstr = Regex.Match(result.Html, regex);
while (mstr.Success && count < )
{
proxyIp.Add(mstr.Groups[].Value + ":" + mstr.Groups[].Value);
mstr = mstr.NextMatch();
count++;
} return true;
}
else
{
return false;
}
}, ); count = ;
para.URL = "http://www.66ip.cn/nmtq.php?getnum=10&isp=0&anonymoustype=3&start=&ports=&export=&ipaddress=&area=1&proxytype=2&api=66ip"; // 66ip
RetryFunc(() =>
{
HttpResult result = http.GetHtml(para);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
string regex = @"(\d+\.\d+\.\d+\.\d+):(\d+)<br/>";
Match mstr = Regex.Match(result.Html, regex);
while (mstr.Success && count < )
{
proxyIp.Add(mstr.Groups[].Value + ":" + mstr.Groups[].Value);
mstr = mstr.NextMatch();
count++;
} return true;
}
else
{
return false;
}
}, ); foreach (var item in proxyIp)
{
proxyIpQueue.Enqueue(item);
}
}
#endregion