Retrieve Web Data

I want to get prices today from the link

I installed HTMLAgilityPack, but I can’t grab the document table right away to print a line whose first td element contains today's date in the format dd-mmm-yy

Can someone point me in the right direction a piece of code?

I currently have:

HtmlDocument doc = new HtmlDocument();
doc.Load("http://lbma.org.uk/pages/printerFriendly.cfm?thisURL=index.cfm&title=gold_fixings&page_id=53&show=2012&type=daily");

foreach(HtmlNode tr in doc.DocumentNode.SelectNodes("tr"))
{

}
+3
source share
3 answers

Fun This page is horribly distorted by Html, so I see your problem. However, I would not touch the XPath with a 10-foot pole. Link makes life a lot easier.

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://lbma.org.uk/pages/printerFriendly.cfm?thisURL=index.cfm&title=gold_fixings&page_id=53&show=2012&type=daily");

HtmlNode todaysRow = doc.DocumentNode.Descendants("tr").Where(n => n.InnerText.StartsWith(string.Format("{0:dd-MMM-yy}", DateTime.Today), StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
if (todaysRow != null)
{
    List<HtmlNode> cells = todaysRow.Descendants("td").ToList();
    decimal usd = decimal.Parse(cells[1].FirstChild.InnerText);
    decimal gbp = decimal.Parse(cells[2].FirstChild.InnerText);
    // ... etc 
} 
+1
source

XPath. , , , - :

foreach(HtmlNode tr in doc.DocumentNode.SelectNodes("tr[td[1] = '03-Jan-12']"))
{

}    
0

:

Dictionary<string, string> values = new Dictionary<string, string>();
string key, date;
HtmlDocument doc = Load(html);
HtmlNode node = doc.DocumentNode.SelectSingleNode(".//table[@class='pricing detail']");
//this will pull out only the dates, and store them in variable 'date'
foreach(HtmlNode child in node.SelectNodes(".//tr[@class='left']")
{
    date = child.GetInnerText;
}
//this will pull out the dates and the prices, and put them into a mapped data structure for easy (and quick!) referencing
foreach(HtmlNode child in node.SelectNodes(".//tr")
{
    if(child.Attributes.contains("class"))
    {
        key = child.GetInnerText;
    }
    else
    {
        values.Add(key, child.GetInnerText);
    }
}

.

: , foreach() , <tr>. , , node (.. node <table class="pricing detail">. , node ( GetInnerText), ( ). , node , , .

, ,

, . , , , , , .

foreach: , ( ). , , , , .

... HTML. ... -, - , . , , , , , , ,... funner... . -, , , htmlagilitypack. , .

0

All Articles