kylin类库之获取代理IP

  1 namespace Kylin.GetHttpIp
  2 {
  3     public class kylinIp
  4     {
  5         ///爬虫获取网站的高匿代理IP
  6         ///目前使用的网站有:
  7         ///http://www.xdaili.cn/freeproxy
  8         ///http://www.xicidaili.com/nn/
  9         ///http://www.goubanjia.com/free/gngn/index.shtml
 10         ///
 11
 12         ///第一个网站可以抓包:http://www.xdaili.cn/ipagent//freeip/getFreeIps?page=1&rows=10
 13         ///其他几个都要进页面
 14         ///
 15         HttpUtility http;
 16
 17
 18         public kylinIp()
 19         {
 20             http = new HttpUtility();
 21         }
 22
 23
 24         public string GetIPBy_xdaili()
 25         {
 26             string Url = "http://www.xdaili.cn/ipagent//freeip/getFreeIps?page=1&rows=10";
 27             try {
 28                 string Area_Html = http.GetHtmlText(Url);
 29                 JObject Area_Json = (JObject)JsonConvert.DeserializeObject(Area_Html);
 30                 for (int j = 0; j < Area_Json["RESULT"]["rows"].Count(); j++)
 31                 {
 32                     if (Area_Json["RESULT"]["rows"][0]["anony"].ToString() == "高匿")
 33                     {
 34                         string IP = Area_Json["RESULT"]["rows"][0]["ip"].ToString();
 35                         string Duankou = Area_Json["RESULT"]["rows"][0]["port"].ToString();
 36                         try
 37                         {
 38                             WebProxy proxyObject = new WebProxy(IP, int.Parse(Duankou));//str为IP地址 port为端口号 代理类
 39                             HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com.tw/"); // 61.183.192.5
 40                             Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
 41                             Req.Proxy = proxyObject; //设置代理
 42                             Req.Method = "GET";
 43                             HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();
 44                             string StringSub = "";
 45                             string OkStr = "";
 46                             Encoding code = Encoding.GetEncoding("utf-8");
 47                             using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code))
 48                             {
 49                                 string str1 = sr.ReadToEnd();//获取得到的网址html返回数据,这里就可以使用某些解析html的dll直接使用了,比如htmlpaser
 50                                 if (str1.IndexOf(IP) > 0)
 51                                 {
 52                                     return IP + ":" + Duankou;
 53                                 }
 54                             }
 55                         }
 56                         catch { }
 57
 58
 59                     }
 60                 }
 61             }
 62             catch { }
 63
 64             Url = "http://www.xicidaili.com/nn/";
 65             try
 66             {
 67                 string Area_Html = http.GetHtmlText(Url);
 68                 var documenthtml = new JumonyParser().Parse(Area_Html);
 69
 70                 var lists = documenthtml.FindFirst("#ip_list").Find("tr").ToList();
 71
 72                 bool first_ip = true;
 73                 foreach (var list in lists)
 74                 {
 75                     if(first_ip){
 76                         continue;
 77                     }
 78                     var IP_i = list.Find("td").ToList();
 79                     string IP = IP_i[1].ToString();
 80                     string Duankou = IP_i[2].ToString();
 81                     try
 82                     {
 83                         WebProxy proxyObject = new WebProxy(IP, int.Parse(Duankou));//str为IP地址 port为端口号 代理类
 84                         HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com.tw/"); // 61.183.192.5
 85                         Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
 86                         Req.Proxy = proxyObject; //设置代理
 87                         Req.Method = "GET";
 88                         HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();
 89                         string StringSub = "";
 90                         string OkStr = "";
 91                         Encoding code = Encoding.GetEncoding("utf-8");
 92                         using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code))
 93                         {
 94                             string str1 = sr.ReadToEnd();//获取得到的网址html返回数据,这里就可以使用某些解析html的dll直接使用了,比如htmlpaser
 95                             if (str1.IndexOf(IP) > 0)
 96                             {
 97                                 return IP + ":" + Duankou;
 98                             }
 99                         }
100                     }
101                     catch { }
102                 }
103             }
104             catch { }
105
106
107             return "当前暂无可用";
108         }
109     }
110 }

代码

其实就是简单的获取一些网站(分享免费代理),经过自己的检测可用性,然后返回出带端口号的字符串

时间: 2024-08-07 20:33:21

kylin类库之获取代理IP的相关文章

获取代理IP地址

今天在开源中国上看到有个有写了个小程序,用来获取代理IP地址.用的beautifulsoup. 自己动手用正则重写了一下. #!/usr/bin/python import requests import re pattern=re.compile(r'(\d+)\D(\d+)\D(\d+)\D(\d+)\D(\d+)') headers={'Host':"www.ip-adress.com", 'User-Agent':"Mozilla/5.0 (Windows NT 6.

分享一个获取代理ip的python函数

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #coding:utf-8 from bs4 import BeautifulSoup import requests import random def getproxyip(): headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encodi

python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如果模拟请求重启路由器(网络爬虫常用),还有java如何下载图片 - baidu_nod的专栏 - 博客频道 - CSDN.NET undefined [多线程数据采集]使用Jsoup抓取数据+破解屏蔽ip访问. - MiniBu - 开源中国社区 undefined 单linux服务器同时拨多条AD

Python3.x:获取代理ip

Python3.x:获取代理ip 获取代理ip,代码: # python3 # 国内高匿代理IP网站:http://www.xicidaili.com/nn/ # 爬取首页代理IP地址 from bs4 import BeautifulSoup import requests import random # 获取首页IP列表 def get_ip_list(url, headers): web_data = requests.get(url, headers=headers) soup = Be

python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)

python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_useragent库,伪装请求头 from fake_useragent import UserAgent ua = UserAgent() # ie浏览器的user agent print(ua.ie) # opera浏览器 print(ua.opera) # chrome浏览器 print(ua.chro

获取代理IP地址(BeautifulSoup)

前天用正则的方式获取网站的代理IP数据,今天为了学习BeautifulSoup,用BeautifulSoup实现了一下. 1 #!/usr/bin/python 2 3 import requests 4 from bs4 import BeautifulSoup 5 6 7 headers={'Host':"www.ip-adress.com", 8 'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gec

python获取代理IP

利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup from threading import Thread headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'} #定义获取IP函数

php获取代理ip

最近我们的php网站想获得玩家的ip通过简单的$_SERVER["REMOTE_ADDR"]竟然获取到的是127.0.0.1的ip 因为我们的网站是通过nginx代理的,想到可能获得玩家的ip有点不一样吧 看到有通过这个的$_SERVER['HTTP_X_FORWARDED_FOR'],我改了一下代码竟然没有效果 好像没有传过来吗,然后网上找了一下原来nginx也要设置一下的 proxy_set_header X-Forwarded-For $remote_addr; 加这样一行代码后

免费开源的获取代理ip项目

地址:https://github.com/awolfly9/IPProxyTool 根据教程获取ip,项目使用Python语言写的,正好可以让前些日子学了点Python皮毛的我长长见识: ip都是会通过命令实时地获取ip以及删除一些无效的ip,非常有用 里面可以设置通过哪些网站来判断ip的有效性和速度,以及存储的方式(mysql,mongodb),正好适合当前项目的需求,免费非常棒: