forked from sarthakbabbar3/Python_IMDB_Docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (30 loc) · 1.25 KB
/
main.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
import random
import requests
from bs4 import BeautifulSoup
# crawl IMDB Top 250 and randomly select a movie
URL = 'http://www.imdb.com/chart/top'
def main():
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')
#soup = BeautifulSoup(response.text, 'lxml') # faster
# print(soup.prettify())
movietags = soup.select('td.titleColumn')
inner_movietags = soup.select('td.titleColumn a')
ratingtags = soup.select('td.posterColumn span[name=ir]')
def get_year(movie_tag):
moviesplit = movie_tag.text.split()
year = moviesplit[-1] # last item
return year
years = [get_year(tag) for tag in movietags]
actors_list =[tag['title'] for tag in inner_movietags] # access attribute 'title'
titles = [tag.text for tag in inner_movietags]
ratings = [float(tag['data-value']) for tag in ratingtags] # access attribute 'data-value'
n_movies = len(titles)
while(True):
idx = random.randrange(0, n_movies)
print(f'{titles[idx]} {years[idx]}, Rating: {ratings[idx]:.1f}, Starring: {actors_list[idx]}')
user_input = input('Do you want another movie (y/[n])? ')
if user_input != 'y':
break
if __name__ == '__main__':
main()