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;
        }

 

posted @ 2014-03-03 15:32  zhaocj  阅读(6291)  评论(0编辑  收藏  举报