首页 > 学院 > 开发设计 > 正文

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

2019-11-17 03:19:33
字体:
来源:转载
供稿:网友

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

官网地址:htmlagilitypack

百度网盘下载地址:点击

使用方法:

  1.引用HtmlAgilityPack.dll文件

2.引用命名空间:

using HtmlAgilityPack;

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

     static void Main(string[] args)        {            string html = GetHtml("http://www.vevb.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;        }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表