Fuppiz
Fuppiz

Reputation: 21

get dynamic information from web page in c#?

I have dynamic information on a web page and I wan to get it as a string.

Website : http://geupdater.com/items/377-raw-lobster

There is the "today" xx.xx% and the price information that changes as well. I found it in the source of the page and I know that I can get the source page then start chopping off parts of it but I don't think that its the most efficient way.

this is my attempt but I think its kind of a failure

string pricePLine;
string[] finditems = new string[] { @"<td class=""rise"">", @"<span id=""exactPrice"" class=""right"">" }; // the part in the HTML file that dosen't change 

private void button1_Click(object sender, EventArgs e)
{
    WebClient web = new WebClient();
    string URL = textBox1.Text;
    string[] result = web.DownloadString(URL).Split('\n');

    collectData(result, finditems[0]);

    textBox1.Clear();
    textBox1.Focus();
}

private void collectData(string[] data, string shitToFind)
{
    foreach (string item in data)
    {
        if (item.Contains(shitToFind))
        {

        }
    }
}

Upvotes: 1

Views: 120

Answers (1)

EZI
EZI

Reputation: 15354

When you parse an html page, use a real html parser like HtmlAgilityPack

var html = new WebClient().DownloadString("http://geupdater.com/items/377-raw-lobster");
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var node = doc.DocumentNode.SelectNodes("//article[@class='last half item']");
var values = node.Descendants("th")
                .Zip(node.Descendants("td"), (x, y) => new { Name = x.InnerText, Value = y.InnerText })
                .ToDictionary(x => x.Name, x => x.Value);

Upvotes: 1

Related Questions