我正在用HtmlAgilityPack做实验。目前,我正试图从C#中的一个表中抓取数据。我会用xPath来做这件事,但有些事情似乎不对劲。我已经测试了我的xPath查询,它实际上是返回正确的数据,但是我正在尝试从C#中刮取它返回null。有什么想法吗?
class Program
{
static void Main(string[] args)
{
var hw = new HtmlWeb();
HtmlDocument doc = hw.Load("http://www.filesignatures.net/index.php?page=all");
foreach (HtmlNode row in doc.DocumentNode.SelectNodes("//html//body//div[@id='container']//div[@id='body']//center//table//tbody//tr"))
{
Console.WriteLine(row.InnerText);
}
Console.ReadKey();
}
}发布于 2017-04-14 01:06:46
使用xPath很难维护或调试。你可以用LINQ代替。
var hw = new HtmlWeb();
doc = hw.Load("http://www.filesignatures.net/index.php?page=all");
foreach (HtmlNode row in doc.DocumentNode.Descendants("table").FirstOrDefault(_ => _.Id.Equals("innerTable")).Descendants("tr"))
{
Console.WriteLine(row.InnerText);
}
Console.ReadKey();https://stackoverflow.com/questions/43403778
复制相似问题