Repository navigation
Expand file tree
/
Copy pathimdb.py
More file actions
47 lines (42 loc) · 1.7 KB
/
Copy pathimdb.py
File metadata and controls
47 lines (42 loc) · 1.7 KB
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
from bs4 import BeautifulSoup
import requests
import re
import pprint
import time
pp = pprint.PrettyPrinter(indent=2)
# Download IMDB's Top 250 data
url = 'http://www.imdb.com/chart/top'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
movies = soup.select('td.titleColumn')
links = [a.attrs.get('href') for a in soup.select('td.titleColumn a')]
crew = [a.attrs.get('title') for a in soup.select('td.titleColumn a')]
ratings = [b.attrs.get('data-value') for b in soup.select('td.posterColumn span[name=ir]')]
votes = [b.attrs.get('data-value') for b in soup.select('td.ratingColumn strong')]
imdb = []
# Store each item into dictionary (data), then put those into a list (imdb)
for index in range(0, len(movies)):
# Seperate movie into: 'place', 'title', 'year'
movie_string = movies[index].get_text()
movie = (' '.join(movie_string.split()).replace('.', ''))
movie_title = movie[len(str(index))+1:-7]
year = re.search('\((.*?)\)', movie_string).group(1)
place = movie[:len(str(index))-(len(movie))]
# get the image:
response = requests.get('http://www.imdb.com' + links[index])
soup = BeautifulSoup(response.text, 'html.parser')
images=[img.attrs.get('src') for img in soup.select('div.poster a img')]
data = {"movie_title": movie_title,
"year": year,
"place": place,
"star_cast": crew[index],
"rating": ratings[index],
"vote": votes[index],
"link": links[index],
"images": images
}
imdb.append(data)
#time.sleep(1)
pp.pprint(imdb)
#for item in imdb:
# print(item['place'], '-', item['movie_title'], '('+item['year']+') -', 'Starring:', item['star_cast'])