-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.py
266 lines (192 loc) · 9.44 KB
/
crawl.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import requests
from bs4 import BeautifulSoup
import os
import urllib.request
import xlsxwriter
alphabets = ['A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', 'NUM']
alphabets_pages = [
791, 418, 719, 196, 149, 393, 492,
203, 471, 770, 315, 1040, 384, 64, 719, 36,
526, 1248, 610, 80, 130, 118, 5, 143, 130, 87
]
class CastDetails:
def __init__(self):
self.workbook = xlsxwriter.Workbook('movies_details.xlsx')
self.worksheet = self.workbook.add_worksheet()
self.row = 0
self.column = -1
def increaseRow(self):
self.row += 1
def getCastNames(self, individualLink, movieName):
individualLink_source_code = requests.get(individualLink)
individualLink_plain_text = individualLink_source_code.text
soup = BeautifulSoup(individualLink_plain_text, 'html.parser')
newList = []
for link in soup.findAll('li', {'itemprop':'actors'}):
actors = link.a.text
indexOfDots = actors.find('...')
actorNames = actors[0:indexOfDots]
actorNames = actorNames.lower()
actorNames = actorNames.replace(' ', '-')
lengthOfActorNames = len(actorNames)
actorNames = actorNames[1:lengthOfActorNames - 1]
newList.append(actorNames)
joinString = ','
actorNamesListString = joinString.join(newList)
print(actorNamesListString)
print('-----------------------')
self.worksheet.write(self.row, self.column + 9, actorNamesListString)
detailsList = []
length = len(soup.select('ul.crew-list > li'))
for i in range(length):
x = soup.select('ul.crew-list > li')[i].get_text(strip=True)
detailsList.append(x)
# print(detailsList)
y = ['Banner', 'Release Date','Genre','Music Director','Producer','Language', 'Director','Editor','Cinematographer','Screenplay','Shooting Location','Production Designer','Lyricist','Dialogue','Sound','Writer','Costume Designer','Co-producer','Publicity PRO','Music Company','Playback Singers']
self.worksheet.write(self.row, self.column + 1, movieName)
rate = soup.findAll('span', {'class':'ML15'})
if len(rate) == 0:
rating = 0
else:
for r in rate:
rating = r.text
rating = rating.replace('/5', '')
print(rating)
self.worksheet.write(self.row, self.column + 4, rating)
for yVal in y:
for xVal in detailsList:
if yVal in xVal:
xVal = xVal.replace(yVal,'')
if yVal == 'Banner':
print('Banner : ' + xVal)
self.worksheet.write(self.row, self.column + 5, xVal)
if yVal == 'Release Date':
print('Release Date : ' + xVal)
self.worksheet.write(self.row, self.column + 2, xVal)
if yVal == 'Genre':
print('Genre : ' + xVal)
self.worksheet.write(self.row, self.column + 7, xVal)
if yVal == 'Music Director':
print('Music Director : ' + xVal)
self.worksheet.write(self.row, self.column + 22, xVal)
if yVal == 'Producer':
print('Producer : ' + xVal)
self.worksheet.write(self.row, self.column + 8, xVal)
if yVal == 'Language':
print('Language : ' + xVal)
self.worksheet.write(self.row, self.column + 23, xVal)
if yVal == 'Director':
print('Director : ' + xVal)
self.worksheet.write(self.row, self.column + 24, xVal)
if yVal == 'Editor':
print('Editor : ' + xVal)
self.worksheet.write(self.row, self.column + 26, xVal)
if yVal == 'Cinematographer':
print('Cinematographer : ' + xVal)
self.worksheet.write(self.row, self.column + 27, xVal)
if yVal == 'Screenplay':
print('Screenplay : ' + xVal)
self.worksheet.write(self.row, self.column + 28, xVal)
# if yVal == 'Shooting Location':
# print('Shooting Location : ' + xVal)
# self.worksheet.write(self.row, self.column + 5, xVal)
if yVal == 'Production Designer':
print('Production Designer : ' + xVal)
self.worksheet.write(self.row, self.column + 33, xVal)
if yVal == 'Lyricist':
print('Lyricist : ' + xVal)
self.worksheet.write(self.row, self.column + 25, xVal)
if yVal == 'Dialogue':
print('Dialogue : ' + xVal)
self.worksheet.write(self.row, self.column + 29, xVal)
if yVal == 'Sound':
print('Sound : ' + xVal)
self.worksheet.write(self.row, self.column + 31, xVal)
if yVal == 'Writer':
print('Writer : ' + xVal)
self.worksheet.write(self.row, self.column + 10, xVal)
if yVal == 'Costume Designer':
print('Costume Designer : ' + xVal)
self.worksheet.write(self.row, self.column + 13, xVal)
if yVal == 'Co-producer':
print('Co-producer : ' + xVal)
self.worksheet.write(self.row, self.column + 21, xVal)
if yVal == 'Publicity PRO':
print('Publicity PRO : ' + xVal)
self.worksheet.write(self.row, self.column + 14, xVal)
if yVal == 'Music Company':
print('Music Company : ' + xVal)
self.worksheet.write(self.row, self.column + 32, xVal)
if yVal == 'Playback Singers':
print('Playback Singers : ' + xVal)
self.worksheet.write(self.row, self.column + 35, xVal)
def getImageLink(individualLink, fileName, castDetailsObj):
individualLink_source_code = requests.get(individualLink)
individualLink_plain_text = individualLink_source_code.text
soup = BeautifulSoup(individualLink_plain_text, 'html.parser')
for link in soup.findAll('img', {'class':'bh-fixed-cover-image image'}):
imageLink = link['src']
print('Image Link:' + imageLink)
if not os.path.exists('Images'):
os.makedirs('Images')
fileName = fileName.replace(' ', ' ')
fileName = fileName.replace('?','')
fileName = fileName.replace('/','')
fileNameLength = len(fileName)
fileNameInitial = 1
fileName = fileName[fileNameInitial:fileNameLength]
fileName = fileName.replace(' ', '-')
fileName = fileName.lower() + '.jpg'
print(fileName)
urllib.request.urlretrieve(imageLink, 'Images/' + fileName)
castDetailsObj.worksheet.write(castDetailsObj.row, castDetailsObj.column + 3, fileName)
def spider_insidePage(url, page, castDetailsObj):
page_url = url + 'page/' + str(page) + '/'
page_source_code = requests.get(page_url)
page_plain_text = page_source_code.text
soup_page = BeautifulSoup(page_plain_text, 'html.parser')
loop = 1
for link in soup_page.findAll('li', {'role': 'listitem'}):
movieName = link.text
indexOfBracket = movieName.find('(') - 1
movieName = movieName[0:indexOfBracket]
print(movieName)
print('url: ' + link.a['href'])
if loop == 20:
break
individual_link = link.a['href'] + '/cast/'
castDetailsObj.increaseRow()
getImageLink(individual_link, movieName, castDetailsObj)
castDetailsObj.getCastNames(individual_link, movieName)
print('-------------------------------------------------------')
loop += 1
def spider(urlAlphabet, urlAlphabetPages, castDetailsObj):
url = 'http://www.bollywoodhungama.com/directory/movies-list/alphabet/' + str(urlAlphabet) + '/'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'html.parser')
loop = 1
for link in soup.findAll('li', {'role': 'listitem'}):
movieName = link.text
indexOfBracket = movieName.find('(') - 1
movieName = movieName[0:indexOfBracket]
print(movieName)
print('url: ' + link.a['href'])
if loop == 20:
break
individual_link = link.a['href'] + '/cast/'
castDetailsObj.increaseRow()
getImageLink(individual_link, movieName, castDetailsObj)
castDetailsObj.getCastNames(individual_link, movieName)
print('-------------------------------------------------------')
loop += 1
page = 2
while page <= ((urlAlphabetPages // 20) + 1):
spider_insidePage(url, page, castDetailsObj)
page = page + 1
def main():
castDetailsObject = CastDetails()
for i in range(len(alphabets)):
spider(alphabets[i], alphabets_pages[i], castDetailsObject)
main()