Amazon Reviews Sentiment Analysis
Import the Libraries
requests allows you to send HTTP requests to a server that returns a Response Object with all the response data (i.e. HTML). BeautifulSoup (bs4) is used to pull data out of HTML files and convert the data to a BeautifulSoup object, which represents the HTML as a nested data structure. pandas is used for data analysis and manipulation. urllib can be used for many purposes, including reading website content, making HTTP and HTTPS requests, sending request headers, and retrieving response headers. csv module implements classes to read and write tabular data in CSV format.
Review the Web Page’s HTML Structure
We need to understand the structure and contents of the HTML tags within the web pages. We will be using the Amazon webpage and searching for Apple Watch Series 7 GPS + Cellular (shown below). You can find this webpage by selecting this link and scrolling down to the reviews section.
We can scrape this webpage by parsing the HTML of the page and extracting the information needed for our dataset
we will extract the following data elements from the reviews:
Reviewer Names Reviews
Retrieve HTML data and Extract the Data Elements
Create the Dictionary
We will create a dictionary that will contain the data names and values for the data elements that were extracted.
54 44
Create the Data Frame
We need to adjust for missing values in columns. This step will create the data frame as each key was a row and like this, the missing values are actually missing columns which is no problem for pandas (only missing rows lead to ValueError during creation). We need to transpose the data frame (flip the axis) and change the rows to columns. We also need to clean some of the data.
Reviewer Name
David
Makenna Meyers
Reviews
I can’t tell this was a refurb. The packaging was new, looked like it had never been touched. Very pleased.
Good product and very nice size
Reviewer Name
David
Makenna Meyers
Reviews
I can’t tell this was a refurb. The packaging was new, looked like it had never been touched. Very pleased.
Good product and very nice size
Reviewer Name Reviews
0 David \nI can’t tell this was a refurb. The packagin...
1 Makenna Meyers \nGood product and very nice size\n
2 David \nEverything looks and feels new for a resale ...
3 Florencia \nGreat product. Syncs well with my iPhone. Ba...
4 Amazon Customer \nBought this for myself months ago, watch cam...
5 Anthony Taylor \nI love it .\n
6 Austin Richardson \nOrdered this refurbished watch and the watch...
7 Denisnunez \nI can’t even charge my watch right off the b...
8 Amanda Boyd \nGift to my niece who is an RN\n
9 Sheila Larson \nWorks great\n
0
David
I can’t tell this was a refurb. The packaging was new, looked like it had never been touched. Very pleased.
1
Makenna Meyers
Good product and very nice size
2
David
Everything looks and feels new for a resale item
3
Florencia
Great product. Syncs well with my iPhone. Battery life is marginal.
4
Amazon Customer
Bought this for myself months ago, watch came in the original apple packaging with a usb-c charger, amazing quality for the money. decided to purchase again for my girlfriend a couple months later. this time the watch didn’t come in the original apple watch packaging and had a traditional usb charger, still came with the authentic watch band and watch itself, would still say it was very much worth the money and a great buy.
Sentiment Analysis
Sentiment Analysis is a use case of Natural Language Processing (NLP) and comes under the category of text classification. To put it simply, Sentiment Analysis involves classifying a text into various sentiments, such as positive or negative, Happy, Sad or Neutral, etc. Thus, the ultimate goal of sentiment analysis is to decipher the underlying mood, emotion, or sentiment of a text. This is also known as Opinion Mining
Sentiment(polarity=0.5, subjectivity=0.6)
I love it .
We can see the output is categorized between two — Polarity and Subjectivity.
Polarity is a float value within the range [-1.0 to 1.0] where 0 indicates neutral, +1 indicates a very positive sentiment and -1 represents a very negative sentiment.
Subjectivity is a float value within the range [0.0 to 1.0] where 0.0 is very objective and 1.0 is very subjective. Subjective sentence expresses some personal feelings, views, beliefs, opinions, allegations, desires, beliefs, suspicions, and speculations where as Objective sentences are factual.
/tmp/ipykernel_124/512394660.py:7: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
prod_reviews['Review Type'][i]='Neutral'
/shared-libs/python3.9/py/lib/python3.9/site-packages/pandas/core/indexing.py:1637: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self._setitem_single_block(indexer, value, name)
0
David
I can’t tell this was a refurb. The packaging was new, looked like it had never been touched. Very pleased.
1
Makenna Meyers
Good product and very nice size
2
David
Everything looks and feels new for a resale item
3
Florencia
Great product. Syncs well with my iPhone. Battery life is marginal.
4
Amazon Customer
Bought this for myself months ago, watch came in the original apple packaging with a usb-c charger, amazing quality for the money. decided to purchase again for my girlfriend a couple months later. this time the watch didn’t come in the original apple watch packaging and had a traditional usb charger, still came with the authentic watch band and watch itself, would still say it was very much worth the money and a great buy.
5
Anthony Taylor
I love it .
6
Austin Richardson
Ordered this refurbished watch and the watch is great! The main problem I had with it was that it came with an aftermarket charger. This charger WOULD NOT charge the watch. It would start charging, then stop after a few seconds, then start again, and would keep doing this until I pulled it off the charger. I ordered an actual apple charger for the watch and the watch was charged in a matter of an hour or so. I highly suggest ordering an apple charger at the same time you order the refurbished watch.
7
Denisnunez
I can’t even charge my watch right off the bat. Thoroughly disappointed that they weren’t able to have quality control over this. Now I have to drive to town to get the correct charging block.
8
Amanda Boyd
Gift to my niece who is an RN
9
Sheila Larson
Works great