c# 代理IP获取通用方法

 

 

 

 

 

 

 

 

调用:

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

 

返回结果:

 

一下是获取代理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 = 1000 * 10;
            para.Method = "GET";

            int count = 0;
            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 < 20)
                    {
                        proxyIp.Add(mstr.Groups[1].Value + ":" + mstr.Groups[2].Value);
                        mstr = mstr.NextMatch();
                        count++;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }, 10);

            count = 0;
            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 < 10)
                    {
                        proxyIp.Add(mstr.Groups[1].Value + ":" + mstr.Groups[2].Value);
                        mstr = mstr.NextMatch();
                        count++;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }, 10);

            count = 0;
            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 < 10)
                    {
                        proxyIp.Add(mstr.Groups[1].Value + ":" + mstr.Groups[2].Value);
                        mstr = mstr.NextMatch();
                        count++;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }, 10);

            count = 0;
            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 < 10)
                    {
                        proxyIp.Add(mstr.Groups[1].Value + ":" + mstr.Groups[2].Value);
                        mstr = mstr.NextMatch();
                        count++;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }, 10);

            count = 0;
            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 < 10)
                    {
                        proxyIp.Add(mstr.Groups[1].Value + ":" + mstr.Groups[2].Value);
                        mstr = mstr.NextMatch();
                        count++;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }, 10);

            foreach (var item in proxyIp)
            {
                proxyIpQueue.Enqueue(item);
            }
        } 
        #endregion

 

转载于:https://www.cnblogs.com/benbenfishfish/p/5830149.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值