Scraping Rotten Tomatoes score with ImportXML in Google Sheets

Viewed 384

Each year my friends and I do a pool where we guess the Rotten Tomatoes review percentage of a random mix of movies being released in the summer. We have a spreadsheet that keeps track of everyone's guess and calculates scores on the fly. The spreadsheet scrapes Rotten Tomatoes for that percentage. Due to COVID we didn't play last year, but I'm updating the spreadsheet for this summer's movies and I'm having an issue scraping the data.

URL of the movie I'm using in this example: https://www.rottentomatoes.com/m/wrath_of_man

Here is the ImportXML I used in 2019:

=index(ImportXML("https://www.rottentomatoes.com/m/wrath_of_man", "//span[@class='mop-ratings-wrap__percentage']"),1,1)*100

I'm digging through the source on Rotten Tomatoes and things have changed a bit, but I can't quite figure out how to grab the # I want. I've played around a bunch capturing different spans and divs, but I must be missing something.

I'm not sure if this is the right spot for it, but the % appears in these two sections:

<div class="score-icon-critic right">
<span class="icon fresh big"></span>
<span class="percentage big" data-qa="tomatometer">70%</span>
</div>
<div class="score-icon-critic left">
<span class="icon fresh medium"></span>
<span class="percentage medium" data-qa="tomatometer">70%</span>
</div>

Any help would be appreciated! Thanks.

1 Answers

The web page is now built by javascript. The data are no more available by IMPORTXML function. Nevertheless, you can find them in a json inside the source. To get the score, use this script to fetch the json and parse it :

function rottentomatoes() {
  var url='https://www.rottentomatoes.com/m/wrath_of_man'
  var source = UrlFetchApp.fetch(url).getContentText()
  var jsonString = source.match(/(?<=\<script id="score-details-json" type="application\/json"\>).*(?=<\/script>)/g)
  var data = JSON.parse(jsonString)
  var score = data.scoreboard.tomatometerScore
  Logger.log(score)
}
Related