HtmlAgilityPack 学习和笔记
介绍:
http://www.cnblogs.com/bomo/archive/2013/01/28/2879361.html
实战 c#获取外网ip
网址:http://ip138.com/
如图:分析


有个iframe 实际上查看网页源代码是没有ip的 原因浏览器引擎解析会解析src 地址:
所以我们先获取iframe的url 在根据这个地址 取ip (当然也有人说直接用这个url 不好吗?这里考虑的是怕他变化 容错性好一点 将来)
代码:
static void Main(string[] args)
{
string content = GetHtmlStr("http://www.ip138.com/", Encoding.GetEncoding("gb2312"));
var ulr = "";
HtmlDocument document = new HtmlDocument();
document.LoadHtml(content); HtmlNodeCollection targetNodeCollection = document.DocumentNode.SelectNodes(@"//iframe [1]");//取iframe DOM 第一个
if (targetNodeCollection != null && targetNodeCollection.Count == )
{
ulr = targetNodeCollection[].GetAttributeValue("src", "");//类似jq $(xxx).attr("scr") 对应javascript getattribute
}
content = GetHtmlStr(ulr, Encoding.GetEncoding("gb2312"));
document.LoadHtml(content);
HtmlNodeCollection tempText = document.DocumentNode.SelectNodes(@"//center [1]");//取center DOM 第一个
Console.WriteLine(tempText[].InnerText);//这里就没有过滤了
} public static string GetHtmlStr(string url, Encoding en)
{
try
{
WebRequest rGet = WebRequest.Create(url);
rGet.Timeout = ;
using (WebResponse rSet = rGet.GetResponse())
{
using (StreamReader reader = new StreamReader(rSet.GetResponseStream(), en))
{
return reader.ReadToEnd();
}
}
}
catch (WebException)
{
//连接失败
return null;
}
}
结果:

string content = GetHtmlStr("xxxx", Encoding.GetEncoding("gb2312"));
HtmlDocument document = new HtmlDocument();
document.LoadHtml(content);
HtmlNodeCollection targetNodeCollection = document.DocumentNode.SelectNodes(@"//font[@class='small2']");
Console.WriteLine(targetNodeCollection[targetNodeCollection.Count-].InnerText);
HtmlNodeCollection str = document.DocumentNode.SelectNodes("//td[@class='small']//table[1]//tbody[1]//tr[1]//td[2]");
Console.WriteLine(str[].InnerText);
string xpathstring = "//td[@class='small']";
HtmlNodeCollection aa = document.DocumentNode.SelectNodes(xpathstring); //所有找到的节点都是一个集合
foreach (var htmlNode in aa.Where(htmlNode => htmlNode.GetAttributeValue("style", " ") == "word-wrap:break-word;width:100%;left:0;word-break :break-all;margin-right:1px;"))
{
Console.WriteLine(htmlNode.ChildNodes[].InnerText.Replace("\n", string.Empty).Replace("\t", string.Empty));
break;
}
Console.ReadKey();
HtmlAgilityPack 学习和笔记的更多相关文章
- 每天成长一点---WEB前端学习入门笔记
WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...
- DSP28377S - ADC学习编程笔记
DSP28377S - ADC学习编程笔记 彭会锋 2016-08-04 20:19:52 1 ADC类型导致的配置区别 F28377S的ADC类型是Type 4类型,我的理解是不同类型的ADC采 ...
- 学习ReactNative笔记整理一___JavaScript基础
学习ReactNative笔记整理一___JavaScript基础 ★★★笔记时间- 2017-1-9 ★★★ 前言: 现在跨平台是一个趋势,这样可以减少开发和维护的成本.第一次看是看的ReactNa ...
- 深度学习word2vec笔记之算法篇
深度学习word2vec笔记之算法篇 声明: 本文转自推酷中的一篇博文http://www.tuicool.com/articles/fmuyamf,若有错误望海涵 前言 在看word2vec的资料 ...
- 强化学习读书笔记 - 02 - 多臂老O虎O机问题
# 强化学习读书笔记 - 02 - 多臂老O虎O机问题 学习笔记: [Reinforcement Learning: An Introduction, Richard S. Sutton and An ...
- 强化学习读书笔记 - 05 - 蒙特卡洛方法(Monte Carlo Methods)
强化学习读书笔记 - 05 - 蒙特卡洛方法(Monte Carlo Methods) 学习笔记: Reinforcement Learning: An Introduction, Richard S ...
- 强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning)
强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning) 学习笔记: Reinforcement Learning: An Introductio ...
- (转)深度学习word2vec笔记之基础篇
深度学习word2vec笔记之基础篇 声明: 1)该博文是多位博主以及多位文档资料的主人所无私奉献的论文资料整理的.具体引用的资料请看参考文献.具体的版本声明也参考原文献 2)本文仅供学术交流,非商用 ...
- 深度学习word2vec笔记之基础篇
作者为falao_beiliu. 作者:杨超链接:http://www.zhihu.com/question/21661274/answer/19331979来源:知乎著作权归作者所有.商业转载请联系 ...
随机推荐
- YII2如何修改默认控制器/方法
在网上找了非常多的方法,但是都不好使最后自己综合网上所有自己琢磨出来的,见笑了 首先Yii2中在/vendor/yiisoft/yii2/web/Application.php的第28行 public ...
- Spring Security(08)——intercept-url配置
http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...
- Apache配置手札
一.绑定域名到子目录 在httpd.conf文件末尾添加 #不同的域名对应到的目录 <VirtualHost *:80> DocumentRoot "D:\wamp\www\ba ...
- JS组件系列——两种bootstrap multiselect组件大比拼
前言:今天继续来看看bootstrap的另一个组件:multiselect.记得在项目开始之前,博主项目组几个同事就使用哪些js组件展开过讨论,其中就说到了select组件,由于项目的整体风格使用的b ...
- JS正则表达式基本概念
1.正则表达式(Regular Expression) 在JS里是RegExp 两种字义方式 1. var patt1=new RegExp("e"); 2. var patt1 ...
- 上传本地代码到github
第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令git init第二步:将项目的所有文件添加到仓库中git add .如果想添加某个特定的文件,只需把.换成特定的文件名即可第三步:将 ...
- check fasta format
reference: https://www.biostars.org/p/42126/ fasta.y %{ #include <stdio.h> #include <stdlib ...
- evil program
写的一个恶作剧程序,运行后直接重装系统吧,学习交流使用,后果自负. #include <windows.h> #pragma comment( linker, "/subsyst ...
- XInitThreads与XLIB
XInitThreads函数通常需要尽早调用,一般要在XLIB的其他函数前调用 否则XLIB的函数可能会在调用时直接崩溃(多线程程序中) 最好的做法是,在main入口即调用XInitThreads函数
- Java的发展历程
Java的发展历程充满了传奇色彩. 最初,Java是由Sun公司的一个研究小组开发出来的, 该小组起先的目标是想用软件实现对家用电器进行集成控制的小型控制装置. 开始,准备采用C++,但C++太复杂, ...