|
| 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