Krishna Chaitanya M
Krishna Chaitanya M

Reputation: 227

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

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.

Upvotes: 4

Views: 10519

Answers (3)

Simon Mourier
Simon Mourier

Reputation: 138960

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

Upvotes: 1

Oscar Mederos
Oscar Mederos

Reputation: 29843

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

Upvotes: 4

Harri
Harri

Reputation: 2732

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.

Upvotes: 0

Related Questions