How to Master Data Scraping: Python-Powered Extraction of IMDb’s Top Rated Movies

  • Home
  • Data Scraping
  • How to Master Data Scraping: Python-Powered Extraction of IMDb’s Top Rated Movies


IMDb’s collection of top-rated movies offers a glimpse into the cinematic gems cherished by audiences globally. This guide delves into the art of data scraping this coveted list and harnessing its data potential through Python automation.

Getting Started: Preparing the Toolbox for Data Scraping

Before embarking on our scraping journey, we assemble our toolkit. Importing essential libraries like requests, BeautifulSoup, re, and SQLAlchemy lays the foundation for seamless web scraping and database management.

import requests
from bs4 import BeautifulSoup
import re
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

In this snippet, we import requests for making HTTP requests, BeautifulSoup for parsing HTML content, re for handling regular expressions, and SQLAlchemy modules for managing the database operations.

Scraping IMDb’s Top Rated Movies Data: Unveiling the Gems

Our first step involves navigating to IMDb’s top-rated movies page and extracting the HTML content. Leveraging BeautifulSoup, we parse through this content to isolate the movie titles, ratings, and review counts. Regular expressions help refine the extracted data, ensuring accuracy and consistency.

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36'
page = requests.get('', headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')

Here, we set a user agent header to simulate a browser request and then extract the list of top-rated movies from the HTML source code:

li_soup = soup.find_all("li", {"class": "ipc-metadata-list-summary-item"})
imdb_rating_list = []
for movie_soup in li_soup:
    movie_name = movie_soup.find("h3").text
    movie_name = re.match(r"\d+\.\s(.*)", movie_name).group(1)
    rtng_rvw = movie_soup.find("span", {"class": "ipc-rating-star"}).text
    rex_rtng_rvw = re.match("(.*)\\xa0\((.*)\)", rtng_rvw)
    rating =
    review =
    imdb_rating_list.append({"movie_name": movie_name, "rating": rating, "review": review})

We loop through each movie element, extract its name, rating, and review count using regular expressions, and then store this information in a list of dictionaries.

Storing Scraped Data in a Database: Building a Data Repository

With IMDb’s top-rated movies data in hand, our focus shifts to establishing a robust data storage mechanism. SQLAlchemy facilitates the creation of a database and defines a structured format to accommodate our scraped data. Through SQLAlchemy’s ORM (Object-Relational Mapping), we seamlessly map Python objects to database tables, ensuring data integrity and efficiency.

Now, let’s store the scraped data in a SQLite database using SQLAlchemy:

DATABASE_URL = "sqlite:///imdb_movies.db"
engine = create_engine(DATABASE_URL, echo=True)
Base = declarative_base()

class Movie(Base):
    __tablename__ = 'movies'

    id = Column(Integer, primary_key=True)
    movie_name = Column(String)
    rating = Column(String)
    review = Column(String)

Session = sessionmaker(bind=engine)
session = Session()

for movie in imdb_rating_list:
    movie_obj = Movie(**movie)


print("Data inserted successfully into the database!")

we define a Movie class representing the structure of our database table. We create a session and loop through the scraped data, creating Movie objects and adding them to the session. Finally, we commit the transaction and close the session.

Conclusion: Empowering Data Insights through Automation

In concluding our exploration, we reflect on the transformative potential of web scraping and data automation. By mastering techniques like BeautifulSoup and SQLAlchemy, we unlock a wealth of cinematic insights, ripe for analysis and exploration. Whether for research, analysis, or personal projects, the ability to extract and manage IMDb’s top-rated movies data empowers enthusiasts and professionals alike to delve deeper into the realm of cinema.

Elevating the Script: Enhancements and Adaptations

Beyond the confines of this guide lies a realm of possibilities for script enhancement and adaptation. Additional features, such as genre classification, release year analysis, or user reviews aggregation, can augment the script’s capabilities, enriching the extracted data further. Moreover, scheduling regular updates or integrating with data visualization tools broadens its utility, catering to diverse needs and preferences.


In this comprehensive guide, we’ve embarked on a journey through the intricacies of scraping IMDb’s top-rated movies data using Python automation tools. From laying the groundwork to extracting and storing valuable data, each step brings us closer to unlocking a wealth of cinematic insights. As you embark on your data scraping endeavors, may this guide serve as a beacon of knowledge, illuminating pathways to exploration, discovery, and innovation.

Leave A Comment

Your email address will not be published. Required fields are marked *