Skip to main content

How Web Scraping Is Used To Extract Amazon Prime Data Using Selenium And Beautifulsoup?

 How-Web-Scraping-is-Used-to-Extract-Amazon-Prime-Data-Using-Selenium-And-Beautifulsoup

Selenium is a great tool of web scraping, but has some flaws which is normal because it was designed primarily for testing online applications. However, BeautifulSoup was created particularly for web scraping and is also an excellent tool.

But even BeautifulSoup has its own flaws as when data to be scraped is behind the wall and it requires user authentication or some other actions from user.

This is where Selenium may be used to automate user interactions with the website, and Beautiful Soup will be used to scrape the data once we are in the wall.

When BeautifulSoup and Selenium are combined, you get a perfect web scraping tool. Selenium can also scrape data but BeautifulSoup is far better.

We will use BeautifulSoup and Selenium to scrape movie details from Amazon Prime Video in several categories, such as description, name, and ratings, and then filter the movies depending on the IMDB ratings.

Let’s discuss the process of scraping Amazon Prime data.

Firstly, import the necessary modules

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as soup
from time import sleep
from selenium.common.exceptions import NoSuchElementException
import pandas as pd

Make three empty lists to keep track of the movie information.

movie_names = []
movie_descriptions = []
movie_ratings = []

Chrome Driver must be installed to work this program properly. Make sure you install the driver that relates to your browser version of chrome.

Now, define a function called open_site() that opens the sign-in page of Amazon Prime.

def open_site():
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifiactions")
driver = webdriver.Chrome(executable_path='PATH/TO/YOUR/CHROME/DRIVER',options=options)
driver.get(r'https://www.amazon.com/ap/signin?accountStatusPolicy=P1&clientContext=261-1149697-3210253&language=en_US&openid.assoc_handle=amzn_prime_video_desktop_us&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.primevideo.com%2Fauth%2Freturn%2Fref%3Dav_auth_ap%3F_encoding%3DUTF8%26location%3D%252Fref%253Ddv_auth_ret')
sleep(5)
driver.find_element_by_id('ap_email').send_keys('ENTER YOUR EMAIL ID')
driver.find_element_by_id('ap_password').send_keys('ENTER YOUR PASSWORD',Keys.ENTER)
sleep(2)
search(driver)    

Let's create a search() function that looks for the genre specified.

def search(driver):
    driver.find_element_by_id('pv-search-nav').send_keys('Comedy Movies',Keys.ENTER)
    
    
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("scrollTo(0, document.body.scrollHeight);")
        sleep(5)
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            Break
        last_height = new_height
    html = driver.page_source
    Soup = soup(html,'lxml')
    tiles = Soup.find_all('div',attrs={"class" : "av-hover-wrapper"})
    
    for tile in tiles:
        movie_name = tile.find('h1',attrs={"class" : "_1l3nhs tst-hover-title"})
        movie_description = tile.find('p',attrs={"class" : "_36qUej _1TesgD tst-hover-synopsis"})
        movie_rating = tile.find('span',attrs={"class" : "dv-grid-beard-info"})
        rating = (movie_rating.span.text)
        try:
            if float(rating[-3:]) > 8.0 and float(rating[-3:]) < 10.0:
                movie_descriptions.append(movie_description.text)
                movie_ratings.append(movie_rating.span.text)
                movie_names.append(movie_name.text)
                print(movie_name.text, rating)
        except ValueError:
            Pass

The function searches genre and scrolls till the bottom of page because Amazon Prime Video scrolls endlessly as it uses JavaScript executor and then call driver to acquire page_source. This source is then utilized and feeded to BeautifulSoup.

To be sure that the if statement is looking for movies with a rating of more than 8.0 but less than 10.0.

Let's make a pandas data frame to hold all our movie information.

def dataFrame():
	    
details = {
    'Movie Name' : movie_names,
    'Description' : movie_descriptions,
    'Rating' : movie_ratings
}
data = pd.DataFrame.from_dict(details,orient='index')
data = data.transpose()
data.to_csv('Comedy.csv')

Now let’s try the function we already discussed

open_site()

Result

Result

The result you get will not look the same as here it is formatted such as Column width, text wrap, etc. otherwise it will look almost same.

Conclusion

Hence, BeautifulSoup and Selenium work well together and provides best results considering Amazon Prime Video but Python has other tools also like Scrapy and it is also equally strong.

Looking for best web scraping services to get Amazon Prime data? Contact Web Screen Scraping today!

Request for quote!

Comments

Popular posts from this blog

Scrape OTT Media Platform Using Web Scraping

Scrape OTT Media Platform Data  What are OTT Platforms? There have been massive changes in the platform of OTT. There are many over platforms needed for media services or the apps that we used on mobiles for viewing all the video content. These are the services that are offered to users of the internet. These are the main platforms that have changed in the years. It can be started with Amazon Prime Video Streaming across the world. OTT platforms have changed in such a way that it looks at entertainment. Top on the video demands for the platform that can be used lots of data and can crunch a lot of numbers on different levels so that we can provide perfect content to clients. There are many platforms like Amazon Prime,  Netflix,  HotStar is getting scraped, so we are following that process in which you can scrape the data from OTT Media Platforms Crawling. Talking about the data is everywhere and it is used by many companies that will able to make all video content from di...

How Can You Monitor Prices On Car Dealership Websites?

  H Introduction The vehicle industry is flourishing in every country, including the United States. According to NADA, the 16,795 franchise dealers in the United States sold over 8.6 million light-duty cars in 2018. The total value of new car sales has surpassed $500 billion. Dealerships had requested 155 million repairs in total with $58 billion in sales of parts and services. The area-specific automotive dealer directories are not available, so most information must be gathered through personal contacts or a Google search for a specific place. If you want to scrape data about Price Monitoring from car dealer websites, you can utilize Google as well as the keywords that should include "car dealer," as well as the location and the business name of the automobile. You can attempt scraping data from the first several URLs that are not Google-approved. An excel sheet can be used to repeat the process for multiple regions and car companies. The effectiveness and scale of manually...

How To Extract Booking.Com Data For Hotels?

  This tutorial blog will tell you how to extract booking.com data for hotels with Selectorlib as well as Python. You may also use to scrape hotels data from Booking.com. How to Extract Booking.com? Search Booking.com for the Hotels data with conditions like Locations, Room Type, Check In-Check out Date, Total People, etc. Copy the Search Result URL as well as pass that to the hotel scraper. With the scraper, we would download the URL with Python Requests. After that, we will parse the HTML with Selectorlib Template for scraping fields like Location, Name, Room Types, etc. Then the scraper will save data into the CSV file. The hotel scraper will scrape the following data. You can add additional fields also: Hotel’s Name Location Room Type Pricing Pricing For (eg: 2 Adults, 1 Night) Overall Ratings Bed Type Total Reviews Rating Tile Links Installing the Packages Required to Run a Booking Data Scraper We would require these Packages of Python 3 Python Requests to do requests as well ...