1
votes

I am experimenting with the HtmlAgilityPack. Currently I am trying to scrape data from a table in C#. I will be using xPath for this, but something seems not right.. I have tested my xPath query, and it is actually returning the correct data, but wehn I am trying to scrape from C# it returns null. Any ideas?

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();
    }
}
1

1 Answers

0
votes

Using xPath is pretty hard to maintain or debugging. You can use LINQ instead.

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();