Home > Software engineering >  Get decimal value from webpage with C#
Get decimal value from webpage with C#

Time:03-13

There are many threads similar to this one, but I couldn't figure out what I wanted.I want to get decimal value from webpage.

This is the website: enter image description here

I tried the code stated below but it gets only html codes.

 WebClient wb = new WebClient();
 String strPageCode = wb.DownloadString("https://www.lme.com/Metals/Non-ferrous/LME-Aluminium#Trading day summary");
 richTextBox1.Text = strPageCode;

I think I have to inspect page element similar google chrome. For this i tried to selenium nuget package but i couldn't figure out.

Another solution is sendkey CTRL A and CTRL C to copy all page contents. Then extract value from clipboard content. But how can i sendkey without openning browser?

CodePudding user response:

In order to grab these values you have to make a GET request to this API endpoint: /api/trading-data/day-delayed?datasourceId=1a0ef0b6-3ee6-4e44-a415-7a313d5bd771 (Base address: https://www.lme.com).

An example for getting these values:

using System.Text.Json;

HttpClient client = new();
client.BaseAddress = new Uri("https://www.lme.com");
var response = await client.GetStringAsync("/api/trading-data/day-delayed?datasourceId=1a0ef0b6-3ee6-4e44-a415-7a313d5bd771");

var table = JsonSerializer.Deserialize<Table>(response);
var bids = table.Rows.Select(r => r.Values[0]);
var offers = table.Rows.Select(r => r.Values[1]);
// POCO
public class Table
{
    public Row[] Rows { get; set; }
    public string Title { get; set; }
    public string Strapline { get; set; }
    public string SupportingCopy { get; set; }
    public string NoDataMessage { get; set; }
    public int DataType { get; set; }
    public DateTime DateOfData { get; set; }
    public DateTime LookbackDate { get; set; }
    public bool HistoricalDataLookbackEnabled { get; set; }
    public int HistoricalDataLookbackRange { get; set; }
    public int HistoricalDataLookbackUnit { get; set; }
    public int HistoricalDataDisplayPeriod { get; set; }
    public string[] ColumnTitles { get; set; }
    public bool HideColumnTitles { get; set; }
}

public class Row
{
    public DateTime BusinessDateTime { get; set; }
    public string Ric { get; set; }
    public string RowTitle { get; set; }
    public DateTime[] BusinessDates { get; set; }
    public string[] Values { get; set; }
    public string HoverMessage { get; set; }
    public string HoverValue { get; set; }
    public object HoverValues { get; set; }
    public string HoverText { get; set; }
}
  • Related