My objective is to scrape data from the website :
I am specifically interested in extracting the SP500 PE number, which currently stands at 39.57 (at the time of writing). I require this number to be formatted as 39,57, rather than 39.57.
This is my current 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 challenge: While my code successfully retrieves the information, it provides more details than needed. I need to refine the data and convert 39.57 to 39,57.
Here is the full set of information returned by the code:
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