0

I want to use Google Scripts to import the number of ratings of an Amazon product for each star rating (x 5-star-ratings, y 4-star-ratings, z 3-star-ratings, etc.).

Screenshot of Amazon Product Review Page

I use the script below on this webpage (already filtered for 1 star ratings) but I get "No ratings found"

function getRatings(url) {   
  var content = UrlFetchApp.fetch(url).getContentText();
  var match = content.match(/<div data-hook="cr-filter-info-review-rating-count" class="a-row a-spacing-base a-size-base".*>([^<]*)<\/div>/);
  return match && match [1] ? match[1] : 'No ratings found';
}

Instead, if I use this other script on the same page I am able to gather the total number of ratings (but not only the 1 stars).

function totalReviewcount(url) {   
  var content = UrlFetchApp.fetch(url).getContentText();
  var match = content.match(/<span class="a-size-base a-color-secondary".*>([^<]*)<\/span>/);
  return match && match [1] ? match[1] : 'No reviews yet';
}

Can anyone point me in the right direction?

Thanks

Dobrocode
  • 301
  • 1
  • 2
  • 13
  • 2
    At this time the referred question doesn't help. If the ratings are added dynamically then you will have to implement a headless browser but that might not be an easy task. It's better to look for an API or a more specialized tool for web-scrapping. – Rubén Nov 12 '20 at 19:23
  • Thanks for your reply. Can I ask what do you mean when you say "the ratings are added dynamically"? And how can you tell if they are, by looking at the HTML code of the page? – Dobrocode Nov 13 '20 at 17:35
  • Webpages content could be modified by events that are handled by web apis and JavaScript. Those events might not be triggered by grabing the webpage by using `UrlFetchApp.fetch`. By the way some websites don't serve the same content to users than to things like `UrlFetchApp.fetch`. – Rubén Nov 13 '20 at 18:44
  • How can you tell which elements can be grabbed using UrlFetchApp.fetch? This element is grabbed by `UrlFetchApp.fetch` with no problem for example: ` – Dobrocode Nov 18 '20 at 03:28
  • That is not something that can be answered in a comment. Please edit the question so it can be re-opened. – Rubén Nov 18 '20 at 03:39
  • I just edited the question, but it's still not accepting answers. Should I post a new question and link it here? – Dobrocode Nov 18 '20 at 03:42
  • Question aren't opened automatically after they were edited, they are put in a review queue. Please be patient. – Rubén Nov 18 '20 at 03:46
  • In the mean time, please read [Using regular expressions to parse HTML: why not?](https://stackoverflow.com/q/590747/1595451) – Rubén Nov 18 '20 at 03:47

0 Answers0