Python API Tutorials

How to use an API to get Amazon Product Reviews

If you are a seller on Amazon, then having good reviews about your product is vital. Amazon’s product review data gives you a peek into the minds of your customers. But how do you tap into the data effectively? Thankfully, we have a few APIs hosted here at RapidAPI, which can help you. 

The Amazon Product/Reviews/Keywords API is a complete source of information about products listed in Amazon. This API lets you consume the product reviews based on the ASIN (Amazon Standard Identification Number). 

To get a better sense of the data, how about building a word cloud of review comments? With the help of Python, you can create a program that sifts through the review comments for a particular product and presents a word cloud. In this blog post, we show you the step by step instructions on achieving this for your products listed in Amazon. And who knows, it might just open up some interesting revelations about how your customers perceive your product.

Connect to the Amazon Product/Reviews/Keywords API

Is there an API to pull Amazon Product Reviews?

Yes! The Amazon Product/Reviews/Keywords API supports multiple endpoints for querying information about products and their descriptions, price, reviews, and images. 

Follow the steps below to activate this API with your RapidAPI account. 

1. Sign Up for RapidAPI Account

To begin using the Amazon Product/Reviews/Keywords API, you’ll first need to sign up for a free RapidAPI developer account. With this account, you get a universal API Key to access all APIs hosted on RapidAPI.

RapidAPI is the world’s largest API marketplace, with over 10,000 APIs and a community of over 1,000,000 developers. Our goal is to help developers find and connect to APIs to help them build amazing apps.

2. Subscribe to Amazon Product/Reviews/Keywords API

Once signed in, log on to your RapidAPI account and access the Amazon Product/Reviews/Keywords API Console

Now click on the “Pricing” tab and opt-in for the basic subscription that gives you 25 requests to the API for free.

3. Get Amazon Product Reviews

Once subscribed, come back to the “Endpoints” tab on the API console. On the left panel, you can see a list of the endpoints supported by this API.

To get the product’s review data, you have to use the “GET Product Reviews” endpoint. Select this endpoint, and you can see the input parameters in the middle panel. 

The only mandatory parameter expected by this endpoint is the ‘asin’ which is the unique ASIN assigned by Amazon to every product listed in their marketplace.  You can easily find this on any product page on Amazon’s website for all countries. 

Optionally, you can key in the ‘page’ and the ‘country’ to select the page number of review and the country-specific Amazon site where the product is listed.

With the default parameter values, hit the “Test Endpoint” button to trigger the API. Wait for a few seconds, and you should see the response on the right panel. 

This response contains the first page of review comments on a product represented by an ASIN value of B07XQXZXJC. It includes ten reviews. For fetching subsequent reviews, you can trigger the API with specific page numbers.  Searching on the Amazon US site, you can look up this product with the particular ASIN number.

Similarly, you can look up another product, find its ASIN, and feed it into the API to get the page-wise customer’s reviews.

4. Get the Code Snippet for API Invocation

To call this API programmatically via Python, you need a Python library. The API console gives you the option to choose from among many programming languages and libraries. 

By choosing the Python Requests library, you can get the code snippet for invoking the API’s “GET Product Reviews” endpoint.

With this, you now have all the information required to build a Python program that generates the word cloud from review comments.

Converting Amazon Product Reviews to WordCloud with Python

Now you will use the Amazon Product/Reviews/Keywords API to generate a word cloud from product reviews. 

Do you want to see how the word cloud looks like for one of your products on Amazon? 

Then get ready with your favorite code editor and follow the instructions below to write the Python program to generate such a word cloud. There are a few prerequisites to set up for the development environment for executing the program before you proceed.

Prerequisites

You will be using the Python 3 runtime environment for executing this program. Additionally, you also need a few libraries to generate and display the word cloud. Here is the list of all dependencies for the word cloud generation program.

Python 3:  You can download the latest version of the Python 3 platform from the official Python download page. Make sure to install the version as per your operating system and set the system paths to the Python binaries.

Matplotlib: This is a plotting library for Python. Matplotlib provides the base plotting capabilities for rendering the word cloud.  Refer to the installation instructions, or you can alternatively install the library using the pip install matplotlib command.   

word_cloud:  This is a third-party library for generating word cloud using Python.  You can install the library within your Python 3 environment using the command “pip install wordcloud

Once the above three dependencies are taken care of, open a new file in the code editor. Get ready to type in the code and build the Python program, step by step.

 

Step 1: Declare the import statements and globals

To begin with, you have to import the libraries used in the program. It also uses the RapidAPI key to invoke the Amazon Product/Reviews/Keywords API. A global variable is defined to contain the key.

import sys
import json
import requests
import time
from wordcloud import WordCloud, STOPWORDS 
import matplotlib.pyplot as plt


RAPIDAPI_KEY  = "<YOUR_RAPIDAPI_KEY>"

You have to replace the placeholder <YOUR_RAPIDAPI_KEY> with your actual subscription key.

The significance of each of the import statements will become clear as you add the following code. However, notice that we are importing the two libraries, wordcloud and matplotlib, that are the main dependencies for this program. 

Step 2: Define a function for triggering the API

Below the import and global definitions, add a new function named trigger_api( ) for triggering the API.

def trigger_api(product_asin, page_no):

  querystring = {"page": str(page_no) ,  "country":"US" , "asin": product_asin}

  headers = {
    'x-rapidapi-host': "amazon-product-reviews-keywords.p.rapidapi.com",
    'x-rapidapi-key': RAPIDAPI_KEY
  }

  url = "https://amazon-product-reviews-keywords.p.rapidapi.com/product/reviews"

  response = requests.request("GET", url, headers=headers, params=querystring)

  if(200 == response.status_code):
    return json.loads(response.text)
  else:
    return None

The code within this function is replicated from the generated code snippet that you extracted earlier from the API console.  

This function accepts the product’s ASIN and the page number as arguments. The country is set to “US” so the program will only work with the products listed on Amazon’s US website. It invokes the API via the requests library and returns an API response in JSON format. 

Step 3: Define another function for generating the word cloud

This function is responsible for generating and rendering the word cloud from an input string containing all the words. Define the function as follows.

def render_wordcloud(words):

  stopwords = set(STOPWORDS) 

  wc = WordCloud(width = 800, height = 800, 
                background_color ='white', 
                stopwords = stopwords, 
                min_font_size = 10).generate(words) 
  

  plt.figure(figsize = (8, 8), facecolor = None) 
  plt.imshow(wc) 
  plt.axis("off") 
  plt.tight_layout(pad = 0) 
  plt.show()

The wordcloud module creates a new object based on the words contained in words argument. It generates the word cloud image based on specific inputs that dictate the rendered image’s dimensions and basic style.

Afterward, this object wc is passed to the matplotlib plotting module to show the rendered word cloud image.

Step 4: Define the main block to capture user input

It is time to define the main business logic of the program. Since the API relies on the ASIN of the product, you need to take it as user input. 

Let define the main block below the render_wordcloud( ) function.  

if __name__ == "__main__":

  try: 

    comment_words = ''

    input_asin = input("Enter the product ASIN: ")

    print("Getting latest customer reviews for :  " + input_asin)


      except Exception as e:

    print("Major Exception ...Aborting")
    sys.exit(e)

Here we define a variable comment_words. Eventually, this variable will accumulate all the words as part of the customer’s review comments.

The ASIN is typed by the user and stored in input_asin.

The only missing piece of the business logic is fetching the review comments from the Amazon Product/Reviews/Keywords API and extracting the individual words from each review’s text.

Step 5: Implement the business logic for extracting text from reviews

Add the following code below the print statement in the try block.

for page_no in range(1,4):

  print("Fetching reviews for page " + str(page_no) + " of 3")

  time.sleep(5)

  review_list = trigger_api(input_asin,page_no)

  if(None != review_list): 

    for review in review_list["reviews"]:

      review_comment = review["review"]
      review_words = review_comment.split()

      for i in range(len(review_words)):
        review_words[i] = review_words[i].strip(",:;.!?"'").lower()

      comment_words += " ".join(review_words)+" "
    

comment_words = bytes(comment_words, 'utf-8').decode('utf-8', 'ignore')

print("Generating word cloud....")
render_wordcloud(comment_words)

The for loop runs three times for fetching the three pages of reviews for the product. During each loop iteration, the review text is extracted from the API response, stripped of the punctuation marks, and then appended to the comment_words variable.

Finally, the comment_words is cleaned up to remove any non-UTF characters and then passed on to the render_wordcloud( ) function to display the word cloud. 

The limit in iterations of the for loop is fixed to three to keep the input text for word cloud generation under control. You can choose to expand this by choosing a larger value for the range( ) within the loop initiation. Also, note that the sleep of 5 seconds is introduced in the code to take care of the API rate limits resulting from repeated calls in each loop iteration. 

Save the file with a valid name such as ‘review_cloud.py’. 

Step 6: Test the program by generating a word cloud

Head over to the Amazon.com website and search for your favorite product. Locate the ASIN from the product’s detail page, and you are all set to generate the word cloud now. 

Here is how to invoke this Python program for generating the word cloud for the Apple iPad product bearing an ASIN of B07XQYPM2N.

 

Hurray!! You just amassed all the iPad reviews into a concise word cloud. 

Add some more spark to the word cloud

The word cloud looks cool. However, it definitely lacks any practical insight. 

In order to derive a better meaning from the customer’s reviews about a product, you can try a few things. For instance, you can filter out the common words and display the customer’s rating information from the API response. Additionally, you could also show the sentiment of the customer’s review. 

If you are a Python data science enthusiast, then try to render these bits of information on a matplotlib plot along with the word cloud. Go ahead and accept the challenge to make the word cloud more actionable. We can’t wait to see how you enhance the basic word cloud into a practical product review analytics tool.  Good luck!

We are happy to address your comments and queries on this Python program.   

Connect to the Amazon Product/Reviews/Keywords API

FAQ

How do I get product information from Amazon API?

With the help of RapidAPI, you can get the information about all the products listed on all the Amazon country-specific sites. Log on to RapidAPI and search for Amazon APIs. Alternatively, you can also look up the Amazon Product/Reviews/Keywords API.

How do I use Amazon Product API?

Log on to RapidAPI and search for Amazon API. You will get a list of many APIs that provide a product, pricing, offers, and review related information. Upon subscribing to one of the APIs you can test the API endpoint and get the relevant data.

How do I extract an Amazon review?

To extract customer reviews on any product listed on Amazon, you can subscribe to the Amazon Product/Reviews/Keywords API and lookup for the 'Product Review' endpoint. This API is available as part of RapidAPI. Upon signing up for a RapidAPI account, you can subscribe to the API and use it across all Amazon country-specific websites.

 

5/5 - (1 vote)

View Comments

Share
Published by

Recent Posts

Power Up Your Enterprise Hub: New March Release Boosts Admin Capabilities and Streamlines Integrations

We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…

2 weeks ago

Unveiling User Intent: How Search Term Insights Can Empower Your Enterprise API Hub

Are you curious about what your API consumers are searching for? Is your Hub effectively…

2 weeks ago

Rapid Enterprise API Hub Levels Up Custom Branding, Monetization, and Management in February Release

The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…

4 weeks ago

Supercharge Your Enterprise Hub with January’s Release: Search Insights, Login Flexibility, and More!

This January's release brings exciting features and improvements designed to empower you and your developers.…

3 months ago

Enhanced Functionality and Improved User Experience with the Rapid API Enterprise Hub November 2023 Release

Rapid API is committed to providing its users with the best possible experience, and the…

5 months ago

The Power of Supporting Multiple API Gateways in an API Marketplace Platform

In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…

6 months ago