Open In App

Movie recommendation based on emotion in Python

Last Updated : 09 Aug, 2023
Like Article

Movies that effectively portray and explore emotions resonate deeply with audiences because they tap into our own emotional experiences and vulnerabilities. A well-crafted emotional movie can evoke empathy, understanding, and self-reflection, allowing viewers to connect with the characters and their journey on a profound level.

Sentiment Analysis With Python to Classify Movie

One of the underlying targets of movies is to evoke emotions in their viewers. IMDb offers all the movies for all genres. Therefore the movie titles can be scraped from the IMDb list to recommend to the user. IMDb does not have an API, for accessing information on movies and TV Series. Therefore we have to perform scraping.


  • LXML: Python lxml is the most feature-rich and simple to-utilize library for processing XML and HTML data. Python contents are composed to perform numerous errands like Web scraping or scratching and parsing XML.
  • BeautifulSoup: It is a library of Python that is utilized to pull the data from web pages i.e. HTML and XML files. It works with your preferred parser to give colloquial methods for exploring, looking, and changing the parse tree.

What is web Scraping?

Web scraping is a technique used to extract data from websites automatically. It involves using software or scripts to access web pages, retrieve the HTML content, and then parse the HTML to extract the desired information. The extracted data can be saved in a structured format like CSV, JSON, or a database for further analysis or storage.


Open the terminal and write the given command to install BeautifulSoup Associated and the lxml.

pip install beautifulsoup4
pip install lxml

The scraper is written in Python and uses lxml for parsing the webpages. BeautifulSoup is used for pulling data out of HTML and XML files.

Movie Recommendation Based on Emotion

There are 8 classes of emotion that would be effective to classify a text. These are:

‘Anger’, ‘Anticipation’, ‘Disgust’, ‘Fear’, ‘Joy’, ‘Sad’, ‘Surprise’, ‘Trust’

Here these are taken as input and the corresponding movies would be displayed for the emotion. The correspondence of every emotion with the genre of movies is listed below: Sad – Drama Disgust – Musical Anger – Family Anticipation – Thriller Fear – Sportsthe Enjoyment – Thriller Trust – Western Surprise – Film-Noir Based on the input emotion, the corresponding genre would be selected and all the top 5 movies of that genre would be recommended to the user.

Code Implementation


from bs4 import BeautifulSoup as SOUP
import re
import requests as HTTP
# Main Function for scraping
def main(emotion):
    # IMDb Url for Drama genre of movie against emotion Sad
    if(emotion == "Sad"):
    # IMDb Url for Musical genre of movie against emotion Disgust
    elif(emotion == "Disgust"):
    # IMDb Url for Family genre of movie against emotion Anger
    elif(emotion == "Anger"):
    # IMDb Url for Thriller genre of
    # movie against emotion Anticipation
    elif(emotion == "Anticipation"):
    # IMDb Url for Sport genre of
    # movie against emotion Fear
    elif(emotion == "Fear"):
    # IMDb Url for Thriller genre of
    # movie against emotion Enjoyment
    elif(emotion == "Enjoyment"):
    # IMDb Url for Western genre of
    # movie against emotion Trust
    elif(emotion == "Trust"):
    # IMDb Url for Film_noir genre of
    # movie against emotion Surprise
    elif(emotion == "Surprise"):
    # HTTP request to get the data of
    # the whole page
    response = HTTP.get(urlhere)
    data = response.text
    # Parsing the data using
    # BeautifulSoup
    soup = SOUP(data, "lxml")
    # Extract movie titles from the
    # data using regex
    title = soup.find_all(
        "a", attrs={"href": re.compile(r'\/title\/tt+\d*\/')})
    return title
# Driver Function
if __name__ == '__main__':
    emotion = input("Enter the emotion: ")
    a = main(emotion)
    count = 0
    if(emotion == "Disgust" or emotion == "Anger"
       or emotion == "Surprise"):
        for i in a:
            # Splitting each line of the
            # IMDb data to scrape movies
            tmp = str(i).split('>;')
            if(len(tmp) == 3):
            if(count > 13):
            count += 1
        for i in a:
            tmp = str(i).split('>')
            if(len(tmp) == 3):
            if(count > 11):
            count += 1


Enter the emotion: Enjoyment
Mission: Impossible - Dead Reckoning Part One
Sound of Freedom
They Cloned Tyrone
Talk to Me

This script would scrape all the movie titles of the genre corresponding to the input emotion and list them to the user. Web Scraping is highly beneficial in extracting the data and doing analysis on it. Without web scraping, the Internet as you know it really wouldn’t exist. That’s because Google and other major search engines rely upon a sophisticated web scraper to pull the content that will get included in their index. These tools are what makes search engines possible. Applications of Crawling

Article extraction for websites that curate content.Business listings extraction for companies that build databases of leads. Many different types of data extraction are sometimes called data mining. For example, one popular and sometimes controversial use of a web scraper is for pulling prices off of airlines to publish on airfare comparison sites.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads