Skip to content

Commit b958d67

Browse files
authored
Merge pull request #813 from walkhenj/movie_tv_ratings
Movie and TV ratings scraper uploaded
2 parents 09d9ceb + cf52c86 commit b958d67

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

movie_tv_ratings/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# MOVIE AND TV SHOW RATINGS SCRAPER
2+
This script scrapes the ratings of movies and TV shows from the IMDB website.
3+
4+
## INPUT
5+
User input of 'Movies' or 'TV' in command line.
6+
7+
## OUTPUT
8+
It displays a list of the top 250 movies and top 250 TV shows from the IMDB website.
9+
10+
## AUTHORS
11+
Imaaz Ahmad
12+
Waad AlKhenji

movie_tv_ratings/movie_tv_ratings.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import requests
2+
import re
3+
from bs4 import BeautifulSoup
4+
5+
6+
# Gets top 250 movies from IMDB
7+
def scrape_movies():
8+
9+
response = requests.get('http://www.imdb.com/chart/top')
10+
soup = BeautifulSoup(response.text, 'lxml')
11+
12+
movies = soup.select('td.titleColumn')
13+
ratings = [b.attrs.get('data-value') for b in soup.select('td.posterColumn span[name=ir]')]
14+
15+
for i in range(len(movies)):
16+
movie_string = movies[i].get_text()
17+
movie = (' '.join(movie_string.split()).replace('.', ''))
18+
movie_title = movie[len(str(i)) + 1:-7]
19+
print(("| " + str(i + 1)) + (" | " + movie_title) + (" | Rating : " + "{:.1f}".format(float(ratings[i]))))
20+
return
21+
22+
23+
# Gets top 250 TV shows from IMDB
24+
def scrape_tvshows():
25+
page = requests.get("https://www.imdb.com/chart/toptv")
26+
Results = re.findall(r'" alt="(.+?)".*?title="(.*?)".*?strong.*?"(.*?)"', page.text, re.DOTALL)
27+
for i in range(len(Results)):
28+
print("| " + str(i + 1) + " | " + Results[i][0] + " | Rating : " + Results[i][-1][:3])
29+
30+
return
31+
32+
33+
# USER INTERFACE
34+
35+
print("Type 'Movies' to get the Top 250 Movies on IMDB\n")
36+
print("Type 'TV' to get the Top 250 TV Shows on IMDB\n")
37+
print("Type 'exit' to exit\n")
38+
39+
val = input("Type here: ")
40+
while (val):
41+
if val == 'Movies':
42+
globals()['scrape_movies']()
43+
print("\n")
44+
val = input("Type 'Movies' or 'TV' or 'exit': ")
45+
elif val == 'TV':
46+
globals()['scrape_tvshows']()
47+
print("\n")
48+
val = input("Type 'Movies' or 'TV' or 'exit': ")
49+
elif val == 'exit':
50+
val = ''
51+
else:
52+
val = input("Wrong Input. Try Again: ")

0 commit comments

Comments
 (0)