-
Notifications
You must be signed in to change notification settings - Fork 2
/
imdbProductionCo.py
223 lines (166 loc) · 5.18 KB
/
imdbProductionCo.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
215
216
217
218
219
220
221
222
223
#PURPOSE: This will collect the Production companies and ratings from the movie list
import urllib.request
import json
from bs4 import BeautifulSoup
import re
x=0
out = open('C:\\Users\\582406\\Documents\\Courtney 2016\\DataScrapeExample\\Redo\\ProAndRating\\list2.txt','w')
error=open('C:\\Users\\582406\\Documents\\Courtney 2016\\DataScrapeExample\\errorEx.txt','w')
f=open('C:\\Users\\582406\\Documents\\Courtney 2016\\DataScrapeExample\\Redo\\MovieListPart2.txt')
line=f.readline()
while line:
#Default values
mVotes=0
avM=0.0
feVotes=0
avFe=0.0
check1=0
avCheck1=0.0
mCheck1=0
avMCheck1=0.0
feCheck1=0
avFeCheck1=0.0
check2=0
avCheck2=0.0
mCheck2=0
avMCheck2=0.0
feCheck2=0
avFeCheck2=0.0
check3=0
avCheck3=0.0
mCheck3=0
avMCheck3=0.0
feCheck3=0
avFeCheck3=0.0
check4=0
avCheck4=0.0
mCheck4=0
avMCheck4=0.0
feCheck4=0
avFeCheck4=0.0
US=0
avUS=0.0
nonUS=0
avNonUS=0.0
try:
line=line.replace("\n","")
line=line.replace(" ","+")
line=line.replace("\"","")
print (line)
tempURL= 'http://www.omdbapi.com/?t='
tempURL+=line
tempURL+='&y=&plot=short&r=json'
html=urllib.request.urlopen(tempURL)
#Always put two lines
text=html.read().decode('utf-8')
json_obj=json.loads(text)
#------------------------------------------------------------------------
#These are taken from the OMDb API
title=json_obj['Title']
out.write(title + "\t")
id=json_obj['imdbID']
out.write(id+"\t")
#START OF THE PRODUCTION COMPANY -------------------------------------
urlProd="http://www.imdb.com/title/"
urlProd+=id
urlProd+="/companycredits?ref_=tt_dt_co"
readURL=urllib.request.urlopen(urlProd).read()
soup=BeautifulSoup(readURL, "html.parser")
tableStats=soup.find("ul", { "class" : "simpleList"})
#Finds the Production Companies and separates by a comma
for row in tableStats.find_all('a'):
try:
name=row.getText()
name=name.replace("\n","")
out.write(name)
if "<li>":
out.write(",")
print(name)
except Exception as e:
pass
out.write("\t")
#START OF THE RATING ------------------------
urlRating='http://www.imdb.com/title/'
urlRating+=id
urlRating+='/ratings?ref_=tt_ov_rt'
r= urllib.request.urlopen(urlRating)
html=r.read()
soup=BeautifulSoup(html, "html.parser")
tableStats=soup.find("table", { "cellpadding" : "0"})
for row in tableStats.findAll('tr'):
#find all td
col=row.findAll('td')
out.write(col[1].getText() + "\t")
#Ratings 1-10
#---------------------------------------------------------
#SECOND TABLE for Demographics
tableStats=soup.findAll("table", { "cellpadding" : "0"})[1]
for row in tableStats.findAll('tr'):
col=row.findAll('td')
words=col[0].getText()
num=col[1].getText()
average=col[2].getText()
words=words.strip(' \t\n\r')
print("Start:" + words)
print("Number:" + num)
print("Average:" + average)
#Specifically finding these words in the table to collect data from
if words=="Males":
mVotes=num
avM=average
if words== "Females":
feVotes=num
avFe=average
if words== "Aged under 18":
check1=num
avCheck1=average
if words=="Males under 18":
mCheck1=num
avMCheck1=average
if words== "Females under 18":
feCheck1=num
avFeCheck1=average
if words== "Aged 18-29":
check2=num
avCheck2=average
if words== "Males Aged 18-29":
mCheck2=num
avMCheck2=average
if words== "Females Aged 18-29":
feCheck2=num
avFeCheck2=average
if words== "Aged 30-44":
check3=num
avCheck3=average
if words== "Males Aged 30-44":
mCheck3=num
avMCheck3=average
if words== "Females Aged 30-44":
feCheck3=num
avFeCheck3=average
if words== "Aged 45+":
check4=num
avCheck4=average
if words== "Males Aged 45+":
mCheck4=num
avMCheck4=average
if words== "Females Aged 45+":
feCheck4=num
avFeCheck4=average
if words== "US users":
US=num
avUS=average
if words== "Non-US users":
nonUS=num
avNonUS=average
print("Average Males Start: " + str(avM))
print("Words End: " + words + "\n")
except Exception as e:
print("\n")
pass
#Writes to the outfile the ratings in order
out.write(str(mVotes) + "\t" + str(avM) + "\t" + str(feVotes)+ "\t" + str(avFe)+ "\t" + str(check1)+ "\t" + str(avCheck1)+ "\t" + str(mCheck1)+ "\t" + str(avMCheck1) + "\t" + str(feCheck1) + "\t" + str(avFeCheck1)+ "\t" + str(check2)+ "\t" + str(avCheck2)+ "\t" + str(mCheck2)+ "\t" + str(mCheck2)+ "\t" + str(avMCheck2)+ "\t" + str(feCheck2)+ "\t" + str(avFeCheck2)+ "\t" + str(check3)+ "\t" + str(avCheck3)+ "\t" + str(mCheck3)+ "\t" + str(avMCheck3)+ "\t" + str(feCheck3)+ "\t" + str(avFeCheck3)+ "\t" + str(check4)+ "\t"+ str(avCheck4)+ "\t"+ str(mCheck4)+ "\t" + str(avMCheck4)+ "\t"+ str(feCheck4)+ "\t"+ str(avFeCheck4)+ "\t"+ str(US)+ "\t"+ str(avUS)+ "\t"+ str(nonUS) + "\t"+ str(avNonUS) + "\n")
line=f.readline()
f.close
out.close
error.close