4

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.

1
  • This is a FAQ: browsers add mandatory elements to DOM (as head and tbody). Your input source doesn't have any tbody element.
    – user357812
    Mar 6, 2011 at 21:12

3 Answers 3

4

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

1

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);
}
0

XPath for the first cell in Close column is //div[@id='prices']/table/tbody/tr[2]/td[5] and for the second one it's //div[@id='prices']/table/tbody/tr[3]/td[5] and so on.

1
  • HtmlNode node = hd.DocumentNode.SelectSingleNode("//div[@id='prices']/table/tbody/tr[3]/td[5]"); - This is not working...i am still getting a null reference Mar 6, 2011 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.