I'm trying to WebScrap data from a currency website.

I don't really know how to get the specific span that I'm trying to get here.
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using static System.Net.WebRequestMethods;

namespace ReadDataFromWebsite
{
class Program
{
static void Main(string[] args)
{
String url = "https://www.eldolar.info/es-MX/mexico/dia/hoy";
var httpClient = new HttpClient();
var html = httpClient.GetStringAsync(url).Result;
var htmldocument = new HtmlDocument();
htmldocument.LoadHtml(html);

var divs = htmldocument.DocumentNode.Descendants("span")
.Where(node => node.GetAttributeValue("class", "data-x")
.Contains("xTimes")).ToList();



foreach ( var div in divs )
{
Console.WriteLine(div.InnerText.Trim());
}

//var htmlNodes = htmldocument.DocumentNode.SelectSingleNode("//body/h1");

//var dolar = htmldocument.DocumentNode.SelectSingleNode("//tbody/div[@id='tdSF43718']");
//var dolarfin= dolar.InnerText;
//Console.WriteLine(dolarfin);


}
}

}
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using static System.Net.WebRequestMethods;

namespace ReadDataFromWebsite
{
class Program
{
static void Main(string[] args)
{
String url = "https://www.eldolar.info/es-MX/mexico/dia/hoy";
var httpClient = new HttpClient();
var html = httpClient.GetStringAsync(url).Result;
var htmldocument = new HtmlDocument();
htmldocument.LoadHtml(html);

var divs = htmldocument.DocumentNode.Descendants("span")
.Where(node => node.GetAttributeValue("class", "data-x")
.Contains("xTimes")).ToList();



foreach ( var div in divs )
{
Console.WriteLine(div.InnerText.Trim());
}

//var htmlNodes = htmldocument.DocumentNode.SelectSingleNode("//body/h1");

//var dolar = htmldocument.DocumentNode.SelectSingleNode("//tbody/div[@id='tdSF43718']");
//var dolarfin= dolar.InnerText;
//Console.WriteLine(dolarfin);


}
}

}
If you execute my code basically you will get like 4 numbers, I need to get only the second one.
5 Replies
SpReeD
SpReeD8mo ago
The HtmlAgilityPack uses XPath, which is in your commented code, that's the correct way. Find and isolate the correct HTML entity and retrieve the value you need. Besides this, there are plenty of currency API's out there, no need to webscrape.
Pobiega
Pobiega8mo ago
And also... $scrape2
MODiX
MODiX8mo ago
Before scraping: 1. Read this article: https://benbernardblog.com/web-scraping-and-crawling-are-perfectly-legal-right/ 2. Use an API if one is provided, instead of scraping data. 3. Respect the Terms of Service (ToS). 4. Respect the rules of robots.txt.
Benoit Bernard
Web Scraping and Crawling Are Perfectly Legal, Right?
In this post, you'll find out more on the legal aspect of web scraping and crawling, and what possible consequences you might face.
Pobiega
Pobiega8mo ago
Scraping is very often not allowed. Make sure you are not violating any terms and conditions before writing code
Carlos Ortega
Carlos OrtegaOP8mo ago
thanks for the advice!
Want results from more Discord servers?
Add your server