I am trying to obtain values from a table but am a bit confused on how to properly do it. I want to retrieve the Strike/Symbol/Bid/Ask from the following page: http://finance.yahoo.com/q/op?s=MSFT&m=2012-09
For my code, I have tried number of things but maybe I am not undrestanding correctly how to utilize Xpath.
private void optionchainButton_Click(object sender, EventArgs e)
{
string URL = "http://finance.yahoo.com/q/op?s=" + tickerEditBox.Text;
string HtmlFile = @".\localfile.html";
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{
client.Proxy = null;
//client.DownloadFile(URL, @".\localfile.html");
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(URL);
if (htmlDoc.DocumentNode != null)
{
foreach (HtmlNode text in htmlDoc.DocumentNode.SelectNodes("//table/tbody/tr/td/text()"))
{
Console.WriteLine(text.InnerText);
}
}
}
}
For an alternative to HtmlAgilityPack try CsQuery (on nuget as “CsQuery”), you can use CSS selectors and the jQuery API which are may be more familiar and make it easy to do this kind of parsing. Here is how I would do it with CsQuery:
If you are familiar with CSS & jQuery the methods & selectors should make sense except for the
Cq()method in the last loop. This just wraps an element as aCQobject so you can use the jQuery API against it. This is exactly the same as you would do in jQuery with$(row)to wrap a DOM element. That is, when you iterate over a jQuery object, you get actual DOM elements, not more jQuery objects, so if you want to use the jQuery API against each element in a loop, you need to wrap them in jQuery again. This is how you do it in CsQuery. The same loop in jQuery would be coded like:I have tested this code and it works and returns output like this:
…