C# 解析HTML格式字符串(HtmlAgilityPack)

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

C# 解析HTML格式字符串(HtmlAgilityPack)官网地址:htmlagilitypack

百度网盘下载地址:点击

使用方法:

1.引用HtmlAgilityPack.dll文件

2.引用命名空间:

using HtmlAgilityPack;

3.调用(元素查找方式为xpath,用法参见w3school):

static void Main(string[] args) { string html = GetHtml("http://www.w3school.com.cn/xpath/xpath_syntax.asp"); HtmlDocument doc = new HtmlDocument(); doc.LoaDHTML(html); HtmlNode node = doc.DocumentNode; HtmlNode div = node.SelectNodes("//table[@class='dataintable']")[0]; Console.WriteLine(div.InnerHtml); Console.Read(); } static string GetHtml(string url) { WebRequest request = WebRequest.Create(url); WebResponse res = request.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); res.Close(); return html; }

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航