Movie recommendation based on emotion in Python
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.
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.
- 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.
Installation Required
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 5 classes of emotion that would be effective to classify a text. These are:
"Drama", "Action", "Comedy", "Horror", "Crime"
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
import requests
from bs4 import BeautifulSoup
import re
# Dictionary to map emotions to IMDb URLs
URLS = {
"Drama": 'https://www.imdb.com/search/title/?title_type=feature&genres=drama',
"Action": 'https://www.imdb.com/search/title/?title_type=feature&genres=action',
"Comedy": 'https://www.imdb.com/search/title/?title_type=feature&genres=comedy',
"Horror": 'https://www.imdb.com/search/title/?title_type=feature&genres=horror',
"Crime": 'https://www.imdb.com/search/title/?title_type=feature&genres=crime',
}
def main(emotion):
url = URLS.get(emotion)
print("ok", url)
if not url:
print("Invalid emotion.")
return []
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Check for HTTP errors
except requests.RequestException as e:
print(f"Error fetching data: {e}")
return []
soup = BeautifulSoup(response.text, "lxml")
# Extract movie titles
titles = [a.get_text() for a in soup.find_all('a', href=re.compile(r'/title/tt\d+/'))]
return titles
# Driver Function
if __name__ == '__main__':
emotion = input("Enter the emotion: ").strip()
movie_titles = main(emotion)
if not movie_titles:
print("No titles found.")
else:
max_titles = 14 if emotion in ["Drama", "Action", "Comedy", "Horror", "Crime"] else 12
for title in movie_titles[:max_titles]:
print(title)
Output:
Enter the emotion: Action
1. Twisters
2. Descendants: The Rise of Red
3. Deadpool & Wolverine
4. Twister
5. Beverly Hills Cop: Axel F
6. Gladiator II
7. Captain America: New World Order
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.