forked from diegoisla/Python-Programming-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq58.py
47 lines (34 loc) · 1.3 KB
/
q58.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
# Write a Python program to get movie name, year and a brief summary of the top 10 random movies.
__author__ = "Mahtab Alam"
from bs4 import BeautifulSoup
import requests
import random
def get_imd_movies(url):
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
movies = soup.find_all("td", class_="titleColumn")
random.shuffle(movies)
return movies
def get_imd_summary(url):
movie_page = requests.get(url)
soup = BeautifulSoup(movie_page.text, 'html.parser')
return soup.find("div", class_="summary_text").contents[0].strip()
def get_imd_movie_info(movie):
movie_title = movie.a.contents[0]
movie_year = movie.span.contents[0]
movie_url = 'http://www.imdb.com' + movie.a['href']
return movie_title, movie_year, movie_url
def imd_movie_picker():
ctr = 0
print("================================================")
for movie in get_imd_movies('http://www.imdb.com/chart/top'):
movie_title, movie_year, movie_url = get_imd_movie_info(movie)
movie_summary = get_imd_summary(movie_url)
print(movie_title, movie_year)
print(movie_summary)
print("============================================")
ctr = ctr+1
if (ctr == 10):
break
if __name__ == '__main__':
imd_movie_picker()