mancoder
mancoder

Reputation: 15

How to narrow down results from web scraping using Google App Script?

My goal is to scrape the website : https://www.multpl.com/s-p-500-pe-ratio

Where I specifcally want to get the SP500 PE number, that is 39.57 (at the time of writing). I need this number as 39,57, and not 39.57.

This is my code:

function webScraper() {

var webURL = "https://www.multpl.com/s-p-500-pe-ratio";

var response = UrlFetchApp.fetch(webURL);

var $ = Cheerio.load(response.getContentText()); 

var itemsOfInterest = $('.info-left').first().text().trim();


Logger.log(itemsOfInterest)

return itemsOfInterest

}

The issue: My code works, but it gives me too much information. Basically I need to narrow it down. I also need to convert 39.57 to 39,57.

Below is all the info the code gives me:

Current S&P 500 PE Ratio: 39.57

+0.15 (0.39%)

4:00 PM EST, Fri Feb 5

Mean: 15.88

Median: 14.84

Min: 5.31

(Dec 1917)

Max: 123.73 (May 2009)

Price to earnings ratio, based on trailing twelve month “as reported” earnings.
Current PE is estimated from latest reported earnings and current market price.

Source: Robert Shiller and his book Irrational Exuberance for historic S&P 500 PE Ratio.

See also

Shiller PE Ratio

S&P 500 Price to Book Value

S&P 500 Price to Sales Ratio

S&P 500 Earnings Yield

S&P 500 Earnings

Inflation Adjusted S&P 500

Upvotes: 0

Views: 106

Answers (1)

Tanaike
Tanaike

Reputation: 201358

I believe your goal as follows.

  • You want to retrieve 39.57 from the URL of https://www.multpl.com/s-p-500-pe-ratio and convert 39.57 to 39,57.
  • You want to achieve this using Google Apps Script.

Modification points:

  • When I saw the HTML data of the URL, I noticed that the number of 39.57 is included in the JSON object. I thought that when this is used, the value you expected might be able to be simply retrieved.

In this answer, as a workaround, I would like to propose to retrieve the value from the JSON object in the HTML data. When above points are reflected to your script, it becomes as follows.

Modified script:

function webScraper() {
  var webURL = "https://www.multpl.com/s-p-500-pe-ratio";
  var response = UrlFetchApp.fetch(webURL);
  var str = response.getContentText().match(/var d =([\s\S\w]+?);/);
  if (str && str.length > 0) {
    var obj = JSON.parse(str[1].trim());
    var res = obj.endLabel[0].replace(".", ",");  // If you use obj.endLabel[0] , "39.57" is returned.
    console.log(res)  // When this log is checked, "39,57" can be seen.
    return res;
  }
  return "No value.";
}

Note:

  • This modified script is for your current URL of https://www.multpl.com/s-p-500-pe-ratio. So when you use this script for other URL, the value might not be able to be retrieved. Please be careful this.

References:

Upvotes: 2

Related Questions