XPATH how to extract one td at a time from a tbody in HTML using HTML agility pack

html html-agility-pack xpath

Question

I am trying to parse the table from the URL (Google finance) below

http://www.google.com/finance/historical?q=BOM:533278

I am trying to extract only the close values in the close column. But when i try with the XPATH

hd.DocumentNode.SelectSingleNode("//td[@class='rgt']")

I am getting all the nodes of having attribute as class and value of attribute as rgt in one Node.innerText itself.

I need the values one by one, not all at the same time. I must be doing something silly here. Thank you.

Actual XPath found using Firebug is a follows

/html/body/div/div/div[3]/div[2]/div/div[2]
     /div[2]/div/form/div[2]/table/tbody/tr[2]/td[5]

But some how after the form tag...HTMLagility pack is returning null node. Never thought this would take so long to implement.

Accepted Answer

If you're using Firebug or any Firefox extension (like XPather) to obtain the XPath of the elements you need to parse, you might need to remove the tbody tags from the XPath.

Take a look at the following answer here on SO: Why does firebug add <tbody> to <table>?

If you're using HtmlAgilityPack, the XPath returned by Firebug or by any other tool related with Firefox may differ, because the HTML source you're parsing can be different from the HTML source in Firefox.

Sometimes might be useful to open the same page in Internet Explorer 8 and using Developer Tools (F12) do the same you're doing with Firebug, or if not, use another tool like HAP Explorer that can be downloaded from the HtmlAgilityPack page


Popular Answer

There are many ways to do it. Here is one solution, which is based on the Data td (the one withe the 'lm' class):

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
... load the doc ...

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//td[@class='lm']/../td[5]"))
{
    Console.WriteLine("node=" + node.InnerText);
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why