Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c0b0009

Browse filesBrowse files
committed
added moviepicker
1 parent a70c52f commit c0b0009
Copy full SHA for c0b0009

File tree

Expand file treeCollapse file tree

1 file changed

+44
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+44
-0
lines changed
Open diff view settings
Collapse file

‎moviepicker/main.py‎

Copy file name to clipboard
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import random
2+
import requests
3+
from bs4 import BeautifulSoup
4+
5+
# crawl IMDB Top 250 and randomly select a movie
6+
7+
URL = 'http://www.imdb.com/chart/top'
8+
9+
def main():
10+
response = requests.get(URL)
11+
12+
soup = BeautifulSoup(response.text, 'html.parser')
13+
#soup = BeautifulSoup(response.text, 'lxml') # faster
14+
15+
# print(soup.prettify())
16+
17+
movietags = soup.select('td.titleColumn')
18+
inner_movietags = soup.select('td.titleColumn a')
19+
ratingtags = soup.select('td.posterColumn span[name=ir]')
20+
21+
def get_year(movie_tag):
22+
moviesplit = movie_tag.text.split()
23+
year = moviesplit[-1] # last item
24+
return year
25+
26+
years = [get_year(tag) for tag in movietags]
27+
actors_list =[tag['title'] for tag in inner_movietags] # access attribute 'title'
28+
titles = [tag.text for tag in inner_movietags]
29+
ratings = [float(tag['data-value']) for tag in ratingtags] # access attribute 'data-value'
30+
31+
n_movies = len(titles)
32+
33+
while(True):
34+
idx = random.randrange(0, n_movies)
35+
36+
print(f'{titles[idx]} {years[idx]}, Rating: {ratings[idx]:.1f}, Starring: {actors_list[idx]}')
37+
38+
user_input = input('Do you want another movie (y/[n])? ')
39+
if user_input != 'y':
40+
break
41+
42+
43+
if __name__ == '__main__':
44+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.