Skip to content

Commit 77a6f00

Browse files
authored
Merge pull request Python-World#252 from utkarshbajaj/fetch_imdb_ratings
Fetch imdb ratings
2 parents 7081d5b + dd55e6c commit 77a6f00

4 files changed

Lines changed: 124 additions & 27 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Find IMDB Ratings
2+
<!--Remove the below lines and add yours -->
3+
This script is used to fetch the Ratings and Genre of the films in your films folder that match with ones on IMDb, the data is scraped from IMDB's official website and store in a csv file. The csv file can be used for analysis then, sorting acc to rating etc.
4+
5+
Input: -> Path of the directory which contains the films.
6+
7+
Output: -> A new csv file is made - 'film_ratings.csv' which contains the ratings for the films in your directory.
8+
9+
### Prerequisites
10+
<!--Remove the below lines and add yours -->
11+
This program uses and external dependency of 'BeautifulSoup' (for web scraping), 'requests' (for fetching content of the webpage), 'pandas' (to make the csv file), 'os' (to get data from directory). <br>
12+
These libraries can be installed easily by using the following command: pip install -r requirements.txt
13+
14+
### How to run the script
15+
<!--Remove the below lines and add yours -->
16+
-> Install the requirements. <br>
17+
-> Inside the find_IMDb_rating.py, update the directory path. <br>
18+
-> Type the following command: python find_IMDb_rating.py <br>
19+
-> A csv file with rating will be created in the same directory as the python file. <br>
20+
21+
### Screenshot/GIF showing the sample use of the script
22+
<!--Remove the below lines and add yours -->
23+
Folder :
24+
25+
![Screenshot 2020-09-15 at 6 20 55 PM](https://user-images.githubusercontent.com/44445191/93214776-375f7280-f783-11ea-90a3-dcd29a84d7fc.png)
26+
27+
CSV File:
28+
![Screenshot 2020-09-15 at 6 28 24 PM](https://user-images.githubusercontent.com/44445191/93214767-32022800-f783-11ea-893d-7f45240b6dc5.png)
29+
30+
31+
## *Author Name*
32+
<!--Remove the below lines and add yours -->
33+
<a href = "https://github.com/utkarshbajaj"> Utkarsh Bajaj </a>
Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,80 @@
1-
import requests
21
from bs4 import BeautifulSoup
2+
import requests
3+
import json
4+
import pandas as pd
5+
import os
6+
7+
# Setting up session
8+
s = requests.session()
9+
10+
# List contaiting all the films for which data has to be scraped from IMDB
11+
films = []
12+
13+
# Lists contaiting web scraped data
14+
names = []
15+
ratings = []
16+
genres = []
17+
18+
# Define path where your films are present
19+
# For eg: "/Users/utkarsh/Desktop/films"
20+
path = input("Enter the path where your films are: ")
21+
22+
# Films with extensions
23+
filmswe = os.listdir(path)
24+
25+
for film in filmswe:
26+
# Append into my films list (without extensions)
27+
films.append(os.path.splitext(film)[0])
28+
# print(os.path.splitext(film)[0])
29+
30+
for line in films:
31+
# x = line.split(", ")
32+
title = line.lower()
33+
# release = x[1]
34+
query = "+".join(title.split())
35+
URL = "https://www.imdb.com/search/title/?title=" + query
36+
print(URL)
37+
# print(release)
38+
try:
39+
response = s.get(URL)
40+
41+
#getting contect from IMDB Website
42+
content = response.content
43+
44+
# print(response.status_code)
45+
46+
soup = BeautifulSoup(response.content, features="html.parser")
47+
#searching all films containers found
48+
containers = soup.find_all("div", class_="lister-item-content")
49+
for result in containers:
50+
name1 = result.h3.a.text
51+
name = result.h3.a.text.lower()
52+
53+
# Uncomment below lines if you want year specific as well, define year variable before this
54+
# year = result.h3.find(
55+
# "span", class_="lister-item-year text-muted unbold"
56+
# ).text.lower()
57+
58+
#if film found (searching using name)
59+
if title in name:
60+
#scraping rating
61+
rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"]
62+
#scraping genre
63+
genre = result.p.find("span", class_="genre")
64+
genre = genre.contents[0]
65+
66+
#appending name, rating and genre to individual lists
67+
names.append(name1)
68+
ratings.append(rating)
69+
genres.append(genre)
70+
71+
72+
73+
except Exception:
74+
print("Try again with valid combination of tile and release year")
75+
76+
#storing in pandas dataframe
77+
df = pd.DataFrame({'Film Name':names,'Rating':ratings,'Genre':genres})
378

4-
# Defining variables and url
5-
title = str(input("Enter the title of movie/series: ")).lower()
6-
release = str(input("Enter the year of release: ")).lower()
7-
query = "+".join(title.split())
8-
URL = f"https://www.imdb.com/search/title/?title={query}"
9-
10-
s = requests.session() # Setting up session
11-
12-
try:
13-
response = s.get(URL)
14-
soup = BeautifulSoup(response.content, features="lxml")
15-
containers = soup.find_all("div", class_="lister-item-content")
16-
17-
for result in containers:
18-
name = result.h3.a.text.lower()
19-
year = result.h3.find(
20-
"span", class_="lister-item-year text-muted unbold"
21-
).text.lower()
22-
23-
if title in name and release in year:
24-
rating = result.find("div",
25-
class_="inline-block ratings-imdb-rating")[
26-
"data-value"]
27-
print(f"Rating of {name}:", rating)
28-
except Exception:
29-
print("Try again with valid combination of tile and release year")
79+
#making csv using pandas
80+
df.to_csv('film_ratings.csv', index=False, encoding='utf-8')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
beautifulsoup4==4.9.1
2+
bs4==0.0.1
3+
certifi==2020.6.20
4+
chardet==3.0.4
5+
idna==2.10
6+
pandas==1.1.2
7+
python-dateutil==2.8.1
8+
pytz==2020.1
9+
requests==2.24.0
10+
six==1.15.0
11+
soupsieve==2.0.1
12+
urllib3==1.25.10

0 commit comments

Comments
 (0)