Emotion-based movie recommendations in Python

File handling | Python Methods and Functions | Regular Expressions

Install BeautifulSoup and lxml ,
Open terminal and write

 pip install beautifulsoup4 pip install lxml 

The scraper is written in Python and uses lxml to parse web pages. BeautifulSoup is used to extract data from HTML and XML files.

Film-related emotions

There are 8 classes of emotions that would be efficient for classifying text ... These are: Anger, Expectation, Disgust, Fear, Joy, Sad, Surprise, Trust . Here they are taken as input and the corresponding movies will be displayed for emotion. 
The correspondence of each emotion to the genre of cinema is listed below:

Garden — Drama
Disgust — Musical
Anger — Family
Waiting — Thriller
Fear — Sports
Enjoyment — Thriller
Trust — Western
Surprise — Film Noir

Based on emotions, the appropriate genre will be selected, and all 5 best films of this genre will be recommended to the user.

# Python3 movie code
# recommendation based
# emotion

 
# Import web library
# scrapped

from bs4 import BeautifulSoup as SOUP

import re

import requests as HTTP

 
# Main function for scraping  

def main (emotion):

 

  # IMDb URL for the drama genre

# film against emotions Garden

if (emotion = = "Sad" ):

urlhere = ' http://www.imdb.com/search/title?genres=drama& title_type = feature & amp; sort = moviemeter , asc'

  

# IMDb URL for music genre

# film against emotions Disgust

elif (emotion = = "Disgust" ):

urlhere = ' http://www.imdb.com/search/title?genres=musical&title_type=feature&sort = moviemeter , asc'

 

# IMDb URL for Family genre

# film against emotions Anger

  elif (emotion = = " Anger " ):

  urlhere = ' http://www.imdb.com/search/title?genres = family & amp; title_type = feature & amp; sort = moviemeter , asc'

 

# IMDb URL for thriller genre

# film against emotions

  elif (emotion = = "Anticipation" ):

urlhere = ' http://www.imdb.com/search/title ? genres = thriller & amp; title_type = feature & amp; sort = moviemeter , asc'

 

# IMDb Url for Sport genre

  # film against fear emotions

elif (emotion =   = "Fear" ):

urlhere = ' http://www.imdb.com/ search / title? genres = sport & amp; title_type = feature & amp; sort = moviemeter , asc'

 

# IMDb URL for genre thriller

# film against emotions

elif (emotion = = "Enjoyment" ):

  urlhere = ' http://www.imdb.com/search/title?genres = thriller & amp; title_type = feature & amp; sort = moviemeter , asc'

 

# IMDb URL for Western genre

# film against emotions of trust

elif (emotion = = "Trust" ):

urlhere = ' http://www.imdb.com/search/title?genres= western & amp; title_type = feature & amp; sort = moviemeter , asc'

 

# IMDb URL for Film_noir genre

# film against emotions surprise

elif (emotion = = "Surprise" ):

urlhere = ' http://www.imdb.com/search/title?genres=film_noir&title_type=feature&sort=moviemeter , asc'

 

# HTTP request to get data

# whole page

response = HTTP.get (urlhere)

data = response.text

 

  # Parse data with

# BeautifulSoup

soup = SOUP (data, "lxml" )

 

# Extract movie titles from

  # data using regular expressions

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:

 

# Separate each line

# IMDb data for To clean up movies

tmp = str (i) .split ( '& gt ;;' )

 

if ( len (tmp) = = 3 ):

print (tmp [ 1 ] [: - 3 ])

 

if (count & gt;  13 ):

  break

count + = 1

else :

  for i in a :

tmp = str (i) .split ( '& gt;' )

 

  if ( len (tmp) = = 3 ):

print (tmp [ 1 ] [: - 3 ])

 

if (count & gt;  11 ):

  break

count + = 1

This script will scrape all movie titles in the genre corresponding to the entered emotion and make a list for the user.

Web Scraping is very useful for extracting data and analyzing it. Without Web Cleaning, the Internet, as you know, would not really exist. This is because Google and other major search engines use a sophisticated web scraper to extract content to be included in their index. These tools make search engines possible.

Application crawling

  • Article retrieval for sites that curate content.
  • Business list retrieval for companies that create databases of leads.
  • Many different types of data extraction, sometimes referred to as data extraction. For example, one of the popular and sometimes controversial uses of the web scraper — This is a reduction in airline prices for publication on airfare comparison sites.




Get Solution for free from DataCamp guru