-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (62 loc) · 2.28 KB
/
main.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
from requests import get
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
url = "https://www.imdb.com/search/title/?groups=top_1000&ref_=adv_prv"
headers = {"Accept-Language": "en-US, en;q=0.5"}
results = requests.get(url, headers=headers)
soup = BeautifulSoup(results.text, "html.parser")
#initiate data storage
titles = []
years = []
time = []
imdb_ratings = []
metascores = []
votes = []
us_gross = []
movie_div = soup.find_all('div', class_='lister-item mode-advanced')
#our loop through each container
for container in movie_div:
#name
name = container.h3.a.text
titles.append(name)
#year
year = container.h3.find('span', class_='lister-item-year').text
years.append(year)
# runtime
runtime = container.p.find('span', class_='runtime').text if container.p.find('span', class_='runtime').text else '-'
time.append(runtime)
#IMDb rating
imdb = float(container.strong.text)
imdb_ratings.append(imdb)
#metascore
m_score = container.find('span', class_='metascore').text if container.find('span', class_='metascore') else '-'
metascores.append(m_score)
#there are two NV containers, grab both of them as they hold both the votes and the grosses
nv = container.find_all('span', attrs={'name': 'nv'})
#filter nv for votes
vote = nv[0].text
votes.append(vote)
#filter nv for gross
grosses = nv[1].text if len(nv) > 1 else '-'
us_gross.append(grosses)
#pandas dataframe
movies = pd.DataFrame({
'movie': titles,
'year': years,
'timeMin': time,
'imdb': imdb_ratings,
'metascore': metascores,
'votes': votes,
'us_grossMillions': us_gross,
})
#cleaning data
movies['year'] = movies['year'].str.extract('(\d+)').astype(int)
movies['timeMin'] = movies['timeMin'].str.extract('(\d+)').astype(int)
movies['metascore'] = movies['metascore'].astype(int)
movies['votes'] = movies['votes'].str.replace(',', '').astype(int)
movies['us_grossMillions'] = movies['us_grossMillions'].map(lambda x: x.lstrip('$').rstrip('M'))
movies['us_grossMillions'] = pd.to_numeric(movies['us_grossMillions'], errors='coerce')
#add dataframe to csv file named 'movies.csv'
movies.to_csv('movies.csv')