-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMDb.py
214 lines (194 loc) · 7.66 KB
/
IMDb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import requests
import random
from bs4 import BeautifulSoup
import pandas as pd
from emotions import *
# helper function to iterate through multiple elements in an array
def pairwise(iterable):
a = iter(iterable)
return zip(a, a)
'''
Defines the IMDb class that will contain the title and movie rating data from
# a specific genre of movies
'''
class IMDb:
def __init__(self, genre):
self.genre = genre
self.titles = []
self.ratings = []
self.amount = []
self.numTitles = 0
self.getGenre(self.genre)
self.emotion_genre = 0
#self.toCsv()
'''
gatherEmotion(titles, genre)
poulates the results of sentiment analysis in our object
'''
def gatherEmotions(self, titles, genre) :
self.emotion_genre = emotions(self.titles, self.genre)
"""
main(url)
parameters: the url of the IMDb page being looked at
function: passes in the url ofthe IMDb page, parses all the title and
rating data from the url. Rating data has at least 25,000 ratings from users.
"""
def main(self, url):
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
# getting all of our necessary data from our html file
movieTitles = soup.select('h3.lister-item-header a')
movieRating = soup.select('div.inline-block.ratings-imdb-rating')
grossAmount = soup.select('p.sort-num_votes-visible span[genre = nv]')
# obtain all of the necessary data needed
self.titles.extend([title.text for title in movieTitles])
self.ratings.extend([float(rating['data-value']) for rating in movieRating])
# had to separate the right data values, as votes and gross amount were both under the genre nv in span
self.amount.extend(int(gross['data-value'].replace(',', '')) for votes, gross in pairwise(grossAmount))
# print(self.titles)
# print(self.ratings)
# print(self.amount)
"""
toCsv
parameters: the list containing data points
function: converts to csv file
"""
def toCsv(self):
records = []
for (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) in zip(self.titles, self.ratings, self.amount,
self.emotion_genre.fear, self.emotion_genre.anger, self.emotion_genre.anticipation,
self.emotion_genre.trust, self.emotion_genre.surprise, self.emotion_genre.positive, self.emotion_genre.negative,
self.emotion_genre.sadness, self.emotion_genre.disgust, self.emotion_genre.joy, self.emotion_genre.polarity,
self.emotion_genre.subjectivity):
records.append((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o))
df = pd.DataFrame(records, columns=('Title', 'Rating', 'Net Gross', 'fear', 'anger', 'anticipation',
'trust', 'surprise', 'positive', 'negative', 'sadness', 'disgust',
'joy', 'polarity', 'subjectivity'))
df.to_csv(self.genre + '.csv', index=False, encoding='utf-8')
'''
numberOfTitles
parameters: url of website looked at
function: returns number of titles of a particular genre being looked at
'''
def numberOfTitles(self, url):
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
numberTitles = soup.find_all("div", class_="desc")
test = numberTitles[0].text[6:-1]
Titles = int(''.join(filter(str.isdigit, test)))
return Titles
'''
advancePages
parameters: url of website looked at
function: advances the pages of the particular genre until all title data is collected
and stored into the private member variables of the class.
'''
def advancePages(self, temp):
# a is the temp url that is created. B is the starting number for the page
url_pages = lambda a, b: a + '&start=' + str(b) + '&ref_=adv_nxt'
numTitles = self.numberOfTitles(temp)
counter = 1
while numTitles > 50:
counter += 50
anew = url_pages(temp, counter)
self.main(anew)
numTitles -= 50
# is there still more titles? advance one more page
if (numTitles > 0):
counter += 50
anew = url_pages(temp, counter)
self.main(anew)
"""
genre
parameters: the genre currently being looked at for the ratings and title data
function: passes in the url of the IMDb page, parses all the title and
rating data from the url based on the any of the 18 available
inputted genres (on the movie script website).
"""
def getGenre(self, aGenre):
url = 'https://www.imdb.com/search/title/?title_type=feature&num_votes=25000,&genres='
# Change this based on how you want to sort the website. For now it will be based on box office, since
# some movies actually don't have box office data.
url_completed = '&sort=boxoffice_gross_us,desc'
if aGenre == 'action':
temp = url + 'action' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'adventure':
temp = url + 'adventure' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'animation':
temp = url + 'animation' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'comedy':
temp = url + 'comedy' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'crime':
temp = url + 'crime' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'drama':
temp = url + 'drama' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'family':
temp = url + 'family' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'fantasy':
temp = url + 'fantasy' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'horror':
temp = url + 'horror' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'musical':
temp = url + 'musical' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'mystery':
temp = url + 'mystery' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'romance':
temp = url + 'sci-fi' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'short':
temp = url + 'short' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'thriller':
temp = url + 'thriller' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'war':
temp = url + 'war' + url_completed
self.main(temp)
self.advancePages(temp)
return
elif aGenre == 'western':
temp = url + 'western' + url_completed
self.main(temp)
self.advancePages(temp)
return
else:
return 'this genre does not exist!'