|
| 1 | + |
| 2 | +# Write a Python program to get movie name, year and a brief summary of the top 10 random movies. |
| 3 | + |
| 4 | +__author__ = "Mahtab Alam" |
| 5 | + |
| 6 | +from bs4 import BeautifulSoup |
| 7 | +import requests |
| 8 | +import random |
| 9 | + |
| 10 | + |
| 11 | +def get_imd_movies(url): |
| 12 | + page = requests.get(url) |
| 13 | + soup = BeautifulSoup(page.text, 'html.parser') |
| 14 | + movies = soup.find_all("td", class_="titleColumn") |
| 15 | + random.shuffle(movies) |
| 16 | + return movies |
| 17 | + |
| 18 | + |
| 19 | +def get_imd_summary(url): |
| 20 | + movie_page = requests.get(url) |
| 21 | + soup = BeautifulSoup(movie_page.text, 'html.parser') |
| 22 | + return soup.find("div", class_="summary_text").contents[0].strip() |
| 23 | + |
| 24 | + |
| 25 | +def get_imd_movie_info(movie): |
| 26 | + movie_title = movie.a.contents[0] |
| 27 | + movie_year = movie.span.contents[0] |
| 28 | + movie_url = 'http://www.imdb.com' + movie.a['href'] |
| 29 | + return movie_title, movie_year, movie_url |
| 30 | + |
| 31 | + |
| 32 | +def imd_movie_picker(): |
| 33 | + ctr = 0 |
| 34 | + print("================================================") |
| 35 | + for movie in get_imd_movies('http://www.imdb.com/chart/top'): |
| 36 | + movie_title, movie_year, movie_url = get_imd_movie_info(movie) |
| 37 | + movie_summary = get_imd_summary(movie_url) |
| 38 | + print(movie_title, movie_year) |
| 39 | + print(movie_summary) |
| 40 | + print("============================================") |
| 41 | + ctr = ctr+1 |
| 42 | + if (ctr == 10): |
| 43 | + break |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + imd_movie_picker() |
0 commit comments