Web Scraping – Amazon Customer Reviews
In this article, we are going to see how we can scrape the amazon customer review using Beautiful Soup in Python.
- bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the below command in the terminal.
pip install bs4
- requests: Request allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.
pip install requests
To begin with web scarping, we first have to do some setup. Import all the required modules. Get the cookies data for making the request to amazon, without this you can not able to scrape. Create a header that contains your request cookies, without cookies you can not scrape amazon data it always shows some error. This website will provide you, specific user agent.
Pass the URL in the getdata() function(User Defined Function) to that will request to a URL, it returns a response. We are using get method to retrieve information from the given server using a given URL.
Convert that data into HTML code and then Parse the HTML content using bs4.
Syntax: soup = BeautifulSoup(r.content, ‘html5lib’)
- r.content : It is the raw HTML content.
- html.parser : Specifying the HTML parser we want to use.
Now filter the required data using soup.Find_all function.
Note: This is only HTML code or Raw data.
Now since the core setup is done let us see how scraping for a specific requirement can be done.
Scrape customer name
Now find the customer list with span tag where class_ = a-profile-name. You can open the webpage in the browser and inspect the relevant element by pressing right-click as shown in the figure.
You have to pass the tag name and attribute with its corresponding value to the find_all() function.
[‘Amaze’, ‘Robert’, ‘D. Kong’, ‘Alexey’, ‘Charl’, ‘RBostillo’]
Scrape user review:
Now find the customer review as same above methods. Find the unique class name with a specific tag, here we use div tag.
Scraping Production information
Here we will scrape product information like product name, ASIN number, Weight, dimension. By doing this we will use the span tag and with a specific unique class name.
Scraping Review Image:
Here we will extract the image link from the review of the product using the same as the above methods. The tag name and attribute of the tag is passed to findAll() as above.
Saving details into CSV file:
Here we will save the details into the CSV file, We will convert the data into dataframe and then export it into the CSV, Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file.
Syntax : to_csv(parameters)
- path_or_buf : File path or object, if None is provided the result is returned as a string.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course