Home > Software engineering >  Scraping data from a website with Cheerio and Google script
Scraping data from a website with Cheerio and Google script

Time:08-10

considering the website: https://www.coindesk.com/price/bitcoin/

I'm trying to extract the value $23,073.15 (that changes continuosly) of the following span class.

span >$23,073.15</span

I tried the following code with Google script using Cheerio without success:

var URL = "https://www.coindesk.com/price/bitcoin/";
var response = UrlFetchApp.fetch(URL);
var $ = Cheerio.load(response.getContentText());
var itemsOfInterest = $('span.typography__StyledTypography-owin6q-0 fZpnIj').text();```


any help about?

CodePudding user response:

When I saw the HTML data from your URL using Google Apps Script, unfortunately, it seems that the data is retrieved from an API by Javascript. By this, in this case, your expected value cannot be retrieved from your URL with cheerio. So, in this answer, I would like to propose retrieving your expected value using the API. The sample script is as follows.

Sample script:

function sample() {
  const url = "https://production.api.coindesk.com/v2/tb/price/ticker?assets=BTC";
  const res = UrlFetchApp.fetch(url);
  const obj = JSON.parse(res.getContentText());
  const value = obj.data.BTC.ohlc.l;
  console.log(value)
}
  • I think that when this script is run, you can see your expected value in the log.
  • Related