HtmlAgilityPack中的XPath

时间:2012-11-29 03:50:56

标签: c# xpath html-agility-pack

我有关于'http://stackoverflow.com/'的跟踪xpat​​h'/ html / body / div [4] / div [2] / div / div / h1',即'Top Questions'标题。

如何在HtmlAgilityPack中使用它?

var wc = new WebClient();
wc.Encoding = Encoding.UTF8;
var html = wc.DownloadString("http://stackoverflow.com/");
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var node = htmlDoc.DocumentNode.SelectNodes("/html/body/div[4]/div[2]/div/div/h1");

返回null

1 个答案:

答案 0 :(得分:1)

适合我。 .Dump()就是在LinqPad中使用它。

var url = "http://stackoverflow.com/";
new HtmlWeb().Load(url)
             .DocumentNode
             .SelectSingleNode("/html/body/div[4]/div[2]/div/div/h1")
             .InnerText.Dump();

结果:

    Top Questions    

有一些间距。