/// <summary>
/// 读取URL数据内容
/// </summary>
/// <param name="url">网址</param>
/// <returns>网站文本内容</returns>
public string HttpGetText(string url)
{
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
Request.Method = "GET";
Request.ContentType = @"application/x-www-form-urlencoded";
Request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Request.Headers.Add(HttpRequestHeader.AcceptLanguage, @"Accept-Language:zh-CN,zh;q=0.8");
Request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0";
Request.Proxy = proxy;
var stream = Request.GetResponse().GetResponseStream();
var read = new StreamReader(stream);
var json = read.ReadToEnd();
read.Close();
stream.Close(); System.Diagnostics.Debug.WriteLine("".PadRight(20, '='));
System.Diagnostics.Debug.WriteLine(json);
System.Diagnostics.Debug.WriteLine("".PadRight(20, '='));
return json;
} public class IPs {
public List<proxy> items = new List<proxy>(); public class proxy {
public string ip;
public int port;
public string address;
public int speed;
public int life;//持续分钟数
public DateTime check_time;
}
}
private void button1_Click(object sender, EventArgs e)
{
var html= HttpGetText("http://www.xicidaili.com/nt");
int i1= html.IndexOf("<table id=\"ip_list\">");
int i2= html.IndexOf("</table>");
string ip_list = html.Substring(i1, i2 - i1+ "</table>".Length);
var find = new Regex(@"<tr.*?>\s*?<td.*?>.*?</td>\s*?<td.*?>(?<ip>.*?)</td>\s*?<td.*?>(?<port>.*?)</td>\s*?<td.*?>\s*?<a.*?>(?<address>.*?)</a>\s*?</td>.*?width:(?<speed>.*?)%.*?<td>(?<life>.*?)</td>.*?<td>(?<check_time>.*?)</td>.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var ips= find.Matches(ip_list);
listView1.BeginUpdate();
foreach (System.Text.RegularExpressions.Match item in ips) {
try
{
var ip = new IPs.proxy();
ListViewItem lvi = new ListViewItem(item.Groups["ip"].Value); ip.ip = item.Groups["ip"].Value;
lvi.SubItems.Add(item.Groups["port"].Value); ip.port = Convert.ToInt32(item.Groups["port"].Value);
lvi.SubItems.Add(item.Groups["address"].Value); ip.address = item.Groups["address"].Value;
lvi.SubItems.Add(item.Groups["speed"].Value); ip.speed = Convert.ToInt32(item.Groups["speed"].Value);
lvi.SubItems.Add(item.Groups["life"].Value); ip.life = conv(item.Groups["life"].Value);
lvi.SubItems.Add(item.Groups["check_time"].Value); ip.check_time = Convert.ToDateTime(item.Groups["check_time"].Value);
listView1.Items.Add(lvi);
IPaddress.items.Add(ip);
}
catch {
LogAdd("转换IP地址信息出错 " + item.Value);
}
}
listView1.EndUpdate();
int conv(string life) {
int a = 1;
if (life.Contains("天")) {
a = 60 * 24;
life = life.Replace("天", "");
}else if (life.Contains("分钟"))
{
a =1;
life = life.Replace("分钟", "");
}
else if (life.Contains("小时"))
{
a = 60;
life = life.Replace("小时", "");
}
return Convert.ToInt32(life)*a;
}
}

  

关键代码就是获取指定网页里的IP代理信息,然后用正则表达式提取出来

本来想着直接将html转换为xml,谁知它网页写的不标准,转换不成功

只有用正则来查找了,效果不错~

代码运行环境: vs2017

当然老版本也可以,将局部函数代码放到外部即可。

效果图:

关键代码部分:

var html= HttpGetText("http://www.xicidaili.com/nt");
int i1= html.IndexOf("<table id=\"ip_list\">");
int i2= html.IndexOf("</table>");
string ip_list = html.Substring(i1, i2 - i1+ "</table>".Length);
var find = new Regex(@"<tr.*?>\s*?<td.*?>.*?</td>\s*?<td.*?>(?<ip>.*?)</td>\s*?<td.*?>(?<port>.*?)</td>\s*?<td.*?>\s*?<a.*?>(?<address>.*?)</a>\s*?</td>.*?width:(?<speed>.*?)%.*?<td>(?<life>.*?)</td>.*?<td>(?<check_time>.*?)</td>.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var ips= find.Matches(ip_list);

  正则表达式推荐一个网址及学习工具:

http://deerchao.net/tutorials/regex/regex.htm#charclass

我本人也记不住 正则表达式  ,需要用的时候现查。

自动获取代理IP信息的例子,含代码,分享哦,的更多相关文章

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

    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...

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

    分享一个获取代理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 Beaut ...

  3. 通过AWS的DHCP自动获取的IP地址是否会发生改变?

    针对您的问题,分析如下:1.在一个VPC内,通过AWS的DHCP自动获取的IP地址,在如何情况下会发生改变?例如我把vpc的内所有100个ec2实例全部关闭,再全部重新打开,是否会发生IP地址变化的情 ...

  4. Python学习笔记六(免费获取代理IP)

    为获取网上免费代理IP,闲的无聊,整合了一下,免费从三个代理网站获取免费代理IP,目的是在某一代理网站被限制时,仍可从可以访问的其他网站上获取代理IP.亲测可用哦!^_^  仅供大家参考,以下脚本可添 ...

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

    python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...

  6. Python 之自动获取公网IP

    Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...

  7. Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

    自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...

  8. 解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题

    解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题 该问题是操作系统权限问题,需要将程序设置为以管理员模式运行和以windows7兼容 ...

  9. Python3.x:获取代理ip以及使用

    Python3.x:获取代理ip以及使用 python爬虫浏览器伪装 #导入urllib.request模块 import urllib.request #设置请求头 headers=("U ...

随机推荐

  1. 1601: [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1342  Solved: 881 [Submit][S ...

  2. C#7的9个新语法

    一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...

  3. Thrift序列化与反序列化的实现机制分析

    Thrift是如何实现序死化与反序列化的,在IDL文件中,更改IDL文件中的变量序号或者[使用默认序号的情况下,新增变量时,将新增的变量不放在IDL文件的结尾,均会导致Thrift文件的反序列后无法做 ...

  4. 在windows上缓存git 密码

    缓存git密码 一搜索 大部分都是在linux上的 . git config --global credential.helper cache 但在windows上pull或者push会报如下错误: ...

  5. 谈谈数据库中MyISAM与InnoDB区别 针对业务类型选择合适的表

    MyISAM:这个是默认类型,它是基于传统的ISAM类型, ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法. ...

  6. Angular 2的12个经典面试问题汇总(文末附带Angular测试)

    Angular作为目前最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手,还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...

  7. (转)Uploadify 3.2 参数属性、事件、方法函数详解

    转自http://blog.sina.com.cn/s/blog_5079086b0101fkmh.html Hallelujah博客 一.属性 属性名称 默认值 说明 auto true 设置为tr ...

  8. 十分钟搭建redis单机版 & java接口调用

    本次单机版redis服务器搭建采用的包为redis-3.0.0.tar.gz,主要是记录下安装的心得,不喜勿喷! 一.搭建redis服务器单机版 1.上传redis-3.0.0.tar.gz到服务器上 ...

  9. CDN,你了解多少?

    大家对CDN并不陌生,工作中或多或少都有所接触,最近也有人问到,在此对CDN相关概念和流程做下总结,希望还能对其他朋友也有所帮助. 一.什么是CDN 维基百科上是这样定义的: CDN:内容分发网络(C ...

  10. 一次SocketException:Connection reset 异常排查

    问题描述 上一期的需求上线之后,线上多了一个异常:Connection reset.如下: [2017-03-22 00:45:00 ERROR] [creativeAuditTaskSchedule ...