Flipkart Reviews Sentiment Analysis using Python


One of the biggest online markets in India is Flipkart, where shoppers can buy everything from gadgets to apparel. Any commercial service must examine the tone of the evaluations in order to enhance their services as a result of the growing number of consumers and their feedback. To detect whether a text exhibits a positive, negative, or neutral attitude, Sentiment Analysis is a Natural Language Processing approach which we will be examining using Python to conduct sentiment analysis on product reviews in this technical blog.

Installation and Syntax

To perform sentiment analysis on Flipkart reviews, we will need to install some Python libraries.

pip install beautifulsoup4
pip install textblob
pip install requests

The boilerplate code for the application is a bit like this −

import requests
from bs4 import BeautifulSoup

url = "https://www.flipkart.com/<product_name>/product-reviews/<product_id>?page=<page_number>"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

Algorithm

The algorithm for performing sentiment analysis on Flipkart reviews using TextBlob library is as follows −

  • Extract Flipkart reviews using web scraping techniques.

  • Preprocess the reviews by removing unwanted characters, numbers, and punctuations.

  • Perform sentiment analysis on preprocessed reviews using TextBlob library.

  • By averaging the sentiment ratings of each review, you determine the reviews' overall sentiment.

Example

import requests
from bs4 import BeautifulSoup
from textblob import TextBlob

url = "https://www.flipkart.com/beston-37-keys-piano-keyboard-toy-microphone-usb-power-cable-sound-recording-function-analog-portable/product-reviews/itmfgdhqpccb9hqb?pid=MKDFGDJXZYND3PSZ&lid=LSTMKDFGDJXZYND3PSZKIDQV7&marketplace=FLIPKART&page=5"
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
reviews = soup.find_all('div', {'class': 't-ZTKy'})

for review in reviews[:5]:
   text = review.text
   text = ' '.join(text.split())
   text = ''.join(e for e in text if e.isalnum() or e.isspace())
   sentiment_score = TextBlob(text).sentiment.polarity
   print(text)
   print(sentiment_score)
   if sentiment_score > 0.2:
      print("Analysis: Positive")
   elif sentiment_score < 0.2:
      print("Analysis: Negative")
   else:
      print("Analysis: Neutral")
   print("="*20)

Output

Third class or lower class product I dont want to abuse but product and seller deserve it Product stopped working after 1 month useShameful for flipkart to keep such type cheap products Quality of flipkart is really degrade and selling products like street vendors Third class qualityREAD MORE
0.18333333333333335
Analysis: Negative
====================
Ok ok productREAD MORE
0.5
Analysis: Positive
====================
Nice but price highREAD MORE
0.55
Analysis: Positive
====================
My piano was problemREAD MORE
0.5
Analysis: Positive
====================
Vary bad prodectREAD MORE
-0.09999999999999992
Analysis: Negative
====================

The script has 90% accuracy of prediction when compared to reviews below

Explanation

  • This Python code demonstrates sentiment analysis of reviews for a product listed on Flipkart, a popular online marketplace. The program fetches the reviews from the product page and analyzes the sentiment of each review.

  • The first step is to import the necessary modules - requests, BeautifulSoup, and TextBlob. The requests module is used to fetch the HTML content of the webpage, BeautifulSoup is used to parse the HTML content, and TextBlob is used for sentiment analysis.

  • Next, the URL of the product page is assigned to the variable url. The requests.get() function is used to fetch the HTML content of the webpage. The response variable stores the response object.

  • Then, the BeautifulSoup function is used to parse the HTML content of the response object. The html.parser is used as the parser.

  • The soup.find_all() function is used to find all the reviews on the page. The reviews are identified by the div tag with the class attribute set to t-ZTKy. The reviews variable stores all the review elements.

  • The following step involves looping through the first five reviews. Using the review.text method, the text of each review is retrieved while looping through the first five reviews and the extracted text is cleaned up by eliminating whitespace and numeric characters for a precise prediction

  • The TextBlob function is used to create a TextBlob object from the cleaned text. The sentiment.polarity property of the TextBlob object returns the sentiment score of the review, which is a value between -1 and 1.

  • The sentiment score is then printed along with an analysis of the sentiment. If the sentiment score is greater than 0.2, the review is classified as positive. If the sentiment score is less than 0.2, the review is classified as negative.

Applications

By iterating over each page of audits and saving the results in a CSV file or a data collection, this method may be expanded to do opinion analysis on a sizable number of surveys. This may be used by businesses to measure client satisfaction and improve their offers.

Sentiment analysis on Flipkart reviews can help Flipkart to −

  • Understand the sentiment of their customers towards their products and services.

  • Identify the areas of improvement and take necessary actions to enhance customer satisfaction.

  • Compare your offerings to those of the competition after analyzing theirs.

  • Forecast the direction of the market for their goods and services.

Conclusion

In this technical blog, we explored how to perform sentiment analysis on Flipkart reviews using Python. We used web scraping techniques to extract the reviews and TextBlob library to perform sentiment analysis. Sentiment analysis on Flipkart reviews can help Flipkart to improve their services and enhance customer satisfaction.

Updated on: 22-Aug-2023

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements