-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topmovies.py
103 lines (76 loc) · 2.54 KB
/
Topmovies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import requests
from bs4 import BeautifulSoup
import pandas as pd
from tabulate import tabulate
import pyttsx3
import questionary
engine = pyttsx3.init()
engine.setProperty('rate', 195)
engine.setProperty('volume', 1.5)
engine.say("How would you like to sort the list by:")
engine.runAndWait()
choice = questionary.select(
"How would you like to sort the list by:",
choices=["1. By IMDB Rating", "2. By Release Date","3. By Number of Votes"]
).ask()
if choice == "1. By IMDB Rating":
edit_url = "?sort=ir,desc&mode=simple&page=1"
elif choice == "2. By Release Date":
edit_url = "?sort=us,desc&mode=simple&page=1"
elif choice == "3. By Number of Votes":
edit_url = "?sort=nv,desc&mode=simple&page=1"
# Get the HTML from the URL
url = "https://www.imdb.com/chart/top/"+ edit_url
response = requests.get(url)
# Get the HTML from the URL
html = response.content
# Parse the HTML
soup = BeautifulSoup(html, "html5lib")
# Extract the data
def extractor(classes, tag):
l = []
selection_class = classes
topic_title_tags = soup.find_all(tag, {'class': selection_class})
for i in topic_title_tags:
tmp = i.text.strip()
l.append(tmp)
return l
def title_column():
title_l = extractor("titleColumn", "td")
new_l = []
for i in title_l:
l = i.split("\n")
new = l[0].rstrip() + l[1].lstrip()
newer = new.split(".")
new_l.append(newer[1].lstrip())
return new_l
def imdb_column():
imdb_l = extractor("ratingColumn imdbRating", "td")
return imdb_l
def year_column():
year_l = extractor("secondaryInfo", "span")
new_l = []
for i in year_l:
l = i.split("(")
new = l[1].rstrip(")")
new_l.append(new)
return new_l
def link():
base_link = "https://www.imdb.com"
selection_class = "titleColumn"
topic_title_tags = soup.find_all("td", {'class': selection_class})
link_l = []
for i in topic_title_tags:
link = i.find('a')['href']
link_l.append(base_link + link)
return link_l
dict = {'Title': title_column(), 'Year': year_column(),
'IMDB Rating': imdb_column(), 'Link': link()}
df = pd.DataFrame(dict)
engine.say("How many movies do you want to see?")
engine.runAndWait()
n = int(input("\nEnter the number of movies you want to see: "))
engine.say("Here are the top " + str(n) + " movies")
engine.runAndWait()
df1=df.head(n)
print(tabulate(df1, headers='keys', tablefmt='psql'))