Html解析工具-HtmlAgilityPack
这个工具是在暑假的时候发现的。但是最后没用这个工具。不过,这个工具可是非常强悍的。。 HtmlAgilityPack主要就是解析DOM的。常用的基础类其实不多,对解析DOM来说,就只有HtmlDocument和HtmlNode这两个常用的类,还有一个 HtmlNodeCollection集合类。我给出一个抓取我博客首页文章的例子。看代码可能更清楚一点。你可以去看看压缩包里提供的文档。 xpath如果自己写表达式比较麻烦。所以我还找到了这个HtmlAgilityPack提供了的一个xpath辅助工具-HAPExplorer。都给出了地址。 首先看我的例子,抓取我博客的首页文章: using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Diagnostics; using System.Threading.Tasks; using System.IO; using System.Data; using System.Data.SqlClient; using HtmlAgilityPack; namespace leaver { class Program { static void Main(string[] args) { HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load("http://leaver.me/"); HtmlNode node = doc.GetElementbyId("content"); StreamWriter sw = File.CreateText("leaver.txt"); //从根节点选中class=hfeed的节点 string cfeed = node.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]").OuterHtml; HtmlNode hfeed = HtmlNode.CreateNode(cfeed); foreach (HtmlNode child in hfeed.ChildNodes) { if (child.Attributes["id"] == null || child.Attributes["id"].Value.Substring(0, 2) != "po") continue; HtmlNode hn = HtmlNode.CreateNode(child.OuterHtml); Write(sw, String.Format("标题:{0}", hn.SelectSingleNode("//*[@class=\"entry-title\"]").InnerText)); Write(sw, String.Format("日期:{0}", hn.SelectSingleNode("//*[@class=\"byline\"]").InnerText)); Write(sw, String.Format("摘要:{0}", hn.SelectSingleNode("//*[@class=\"entry-summary\"]").InnerText)); Write(sw, "----------------------------------------"); } sw.Close(); Console.ReadLine(); } static void Write(StreamWriter writer, string str) { Console.WriteLine(str); writer.WriteLine(str); } } } 程序运行结果: xpath表达式的具体书写都是需要分析你需要解析的网站源码的。。。。 xpath辅助工具的界面: 下载:HtmlAgilityPack 下载:HAPExplorer