1
1
import requests
2
2
import re
3
- import sys
4
3
from bs4 import BeautifulSoup
5
4
6
- ##### Gets top 250 movies from IMDB
5
+ # Gets top 250 movies from IMDB
7
6
def scrape_movies ():
7
+
8
8
response = requests .get ('http://www.imdb.com/chart/top' )
9
9
soup = BeautifulSoup (response .text , 'lxml' )
10
10
11
11
movies = soup .select ('td.titleColumn' )
12
- ratings = [b .attrs .get ('data-value' )
13
- for b in soup .select ('td.posterColumn span[name=ir]' )]
12
+ ratings = [b .attrs .get ('data-value' ) for b in soup .select ('td.posterColumn span[name=ir]' )]
14
13
15
14
for i in range (len (movies )):
16
15
movie_string = movies [i ].get_text ()
17
16
movie = (' ' .join (movie_string .split ()).replace ('.' , '' ))
18
17
movie_title = movie [len (str (i ))+ 1 :- 7 ]
19
- print ("| " + str (i + 1 ) + " | " + movie_title + " | Rating : " + "{:.1f}" .format (float (ratings [i ])))
20
- return
18
+ print (( "| " + str (i + 1 )) + ( " | " + movie_title ) + ( " | Rating : " + "{:.1f}" .format (float (ratings [i ]) )))
19
+ return
21
20
22
21
23
- ##### Gets top 250 TV shows from IMDB
22
+ # Gets top 250 TV shows from IMDB
24
23
def scrape_tvshows ():
25
24
page = requests .get ("https://www.imdb.com/chart/toptv" )
26
25
Results = re .findall (r'" alt="(.+?)".*?title="(.*?)".*?strong.*?"(.*?)"' , page .text , re .DOTALL )
27
26
for i in range (len (Results )):
28
27
print ("| " + str (i + 1 ) + " | " + Results [i ][0 ] + " | Rating : " + Results [i ][- 1 ][:3 ])
29
-
30
- return
28
+
29
+ return
31
30
32
31
33
- ##### USER INTERFACE #####
32
+ # USER INTERFACE
34
33
35
34
print ("Type 'Movies' to get the Top 250 Movies on IMDB\n " )
36
35
print ("Type 'TV' to get the Top 250 TV Shows on IMDB\n " )
@@ -40,17 +39,13 @@ def scrape_tvshows():
40
39
while (val ):
41
40
if val == 'Movies' :
42
41
globals ()['scrape_movies' ]()
43
-
44
42
print ("\n " )
45
-
46
43
val = input ("Type 'Movies' or 'TV' or 'exit': " )
47
-
48
44
elif val == 'TV' :
49
45
globals ()['scrape_tvshows' ]()
50
46
print ("\n " )
51
47
val = input ("Type 'Movies' or 'TV' or 'exit': " )
52
-
53
48
elif val == 'exit' :
54
49
val = ''
55
50
else :
56
- val = input ("Wrong Input. Try Again: " )
51
+ val = input ("Wrong Input. Try Again: " )
0 commit comments