I want to get Amazon reviews on GAS

Asked 1 years ago, Updated 1 years ago, 46 views

Both JS and GAS are super beginners.

"I would like to realize the function of ""getting the number of Amazon product reviews on a regular basis and sending emails if there is a change in the number of reviews"" using GAS."

Enter the product name, URL, and number of reviews in the spreadsheet in advance.
Items to be checked must already have at least one review.

I wrote the following code, but sometimes it works well and sometimes it doesn't.
Sometimes the number of reviews I get seems to be zero, and I get an email saying it's zero, and the review section of the spreadsheet is blank.
After that, you will get the correct number of reviews and send an email again.

function myFunction(){

  varsheet=SpreadsheetApp.getActiveSheet();

  var product=sheet.getRange(1,1).getValue();
  var URL = sheet.getRange(1,2).getValue();
  var review=sheet.getRange(1,3).getValue();

  var response=UrlFetchApp.fetch(URL);
  varhtmlstr=response.getContentText();

  // Get the number of reviews
  varmyReg=RegExp (/(\d\d?) customer reviews/);
  var result=htmlstr.match(myReg);
  var new_review =RegExp.$1;

  // send an e-mail if the number of reviews has changed
  if(new_review!=review){
    MailApp.sendEmail(
      "******@****.com",
      The number of reviews for model+" has changed.",
      The number of reviews for URL+"\n"+product+" has changed from ["+review+"] to ["+new_review+"]."
    );
    // rewrite the number of reviews
    sheet.getRange(1,3).setValue(new_review);
  }
}

I wonder if Amazon's response is bad and the number of reviews has not been obtained properly.

In addition, I actually checked several items in the for loop, but I took out only what I thought was necessary to solve the problem and rewritten it.

Thank you for your cooperation.

google-apps-script

2022-09-30 21:21

2 Answers

I think I need some additional information.

  • Do I get an error when the number of reviews reaches zero?Or do you have any information?I thought that knowing the html data collected when the number of reviews went to zero would lead to a solution.

  • If the number of reviews goes to zero, will some of the multiple checked items go to zero or will all go to zero?

  • What triggers are the scripts working with?

Will an error occur if the number of reviews reaches zero?Or do you have any information?I thought that knowing the html data collected when the number of reviews went to zero would lead to a solution.

If the number of reviews goes to zero, will some of the items checked be zero or will all be zero?

What triggers does the script work with?

I was not able to test it because I was not sure about the situation when the number of reviews went to zero, but how about specifying the sheet ID directly as shown below without using the getActiveSheet() method?

vars=SpreadsheetApp.openById('sheet ID');

There may be differences in how the script runs, but I hope this will solve the problem because I had less experience in using the Container-bound Script directly instead of gettingActiveSheet() when using it as a trigger.


2022-09-30 21:21

Sometimes it works well and sometimes it doesn't.

Amazon prevents data download access from SpreadSheet by making it work or go wrong.
I don't think SpreadSheet's GAS scraping is realistic.

I think it would be more effective to have the client interact with the browser and distribute it across several PCs.


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.