-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper_meta.py
More file actions
471 lines (424 loc) · 20.8 KB
/
scraper_meta.py
File metadata and controls
471 lines (424 loc) · 20.8 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
from PIL import Image
import time
import logging
from io import BytesIO
import openpyxl.drawing.image
from bs4 import BeautifulSoup
from requests import get
from openpyxl import Workbook, load_workbook, worksheet
from openpyxl import drawing as dr
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
import os
class ScraperBase:
logger = logging.getLogger('ScraperCore')
def __init__(self, url, outputpath, driver):
self.url = url
self.driver = driver
self.outputpath = outputpath
self.output = dict()
@staticmethod
def get_keyword():
return 'base'
def get_data(self):
raise Exception('Base method called')
def run_browser(self):
self.logger.info('Entering in new tab')
self.logger.debug('Opening new tab')
self.driver.switch_to.new_window()
self.logger.debug('Switching handle')
self.driver.switch_to.window(self.driver.window_handles[0])
self.logger.debug('Closing old tab')
self.driver.close()
self.logger.info('Switching handle 2')
self.driver.switch_to.window(self.driver.window_handles[0])
self.get_data()
def process_image(self, image_url):
ext = image_url.split('.')[-1]
self.logger.debug('Image ext: {}'.format(ext))
r = get(image_url.replace('imagesrc:', ''))
if ext == 'svg':
self.process_svg(BytesIO(r.content))
ext = 'png'
image = Image.open('saved-image.png')
else:
image = Image.open((BytesIO(r.content)))
img_path = 'saved-image.{}'.format(ext)
image = image.resize((140, 90))
image.save(img_path)
return img_path
@staticmethod
def process_number(numstr):
p_str = numstr.replace(',', '')
if 'k' in p_str.lower():
p_str = str(int(float(p_str.replace('K', '').replace('k', '')) * 1000))
if 'm' in p_str.lower():
p_str = str(int(float(p_str.replace('M', '').replace('m', '')) * 1000000))
return p_str
def process_svg(self, svg_bin):
self.logger.debug('Processing svg file')
drawing = svg2rlg(svg_bin)
self.logger.debug("Initiating rendering")
renderPM.drawToFile(drawing, 'saved-image.png', fmt='png')
def save_into_file(self):
'''
Save data into file
'''
logger = logging.getLogger(__name__ + '.SaveFile')
if os.path.isfile(self.outputpath):
wb = load_workbook(self.outputpath)
if self.get_keyword() not in wb.sheetnames:
logger.debug('Creating new sheet')
ws = wb.create_sheet(self.get_keyword())
for index, (key, value) in enumerate(self.output.items()):
# Filling column titles
ws.cell(row=1, column=index + 1, value=key)
# Filling first row
if 'imagesrc:' in value:
try:
logger.debug('Processing image')
logger.debug('Changing cell dimensions')
ws.row_dimensions[2].height = 70
ws.column_dimensions[chr(ord('@') + index + 1)].width = 20
img = openpyxl.drawing.image.Image(self.process_image(value))
cell_name = '{0}{1}'.format(chr(ord('@') + index + 1), 2)
logger.debug('Using cell: {}'.format(cell_name))
ws.add_image(img, cell_name)
except Exception as exc:
logger.error('Error with image processing. Entering default value')
logger.debug('Details: {}'.format(str(exc)))
ws.cell(row=2, column=index + 1, value=value.replace('imagesrc:', ''))
else:
ws.cell(row=2, column=index + 1, value=value)
else:
ws = wb[self.get_keyword()]
next_row = len(ws['A']) + 1
for index, value in enumerate(self.output.values()):
if 'imagesrc:' in value:
try:
logger.debug('Processing image')
logger.debug('Changing cell dimensions')
ws.row_dimensions[next_row].height = 70
ws.column_dimensions[chr(ord('@') + index + 1)].width = 20
img = openpyxl.drawing.image.Image(self.process_image(value))
cell_name = '{0}{1}'.format(chr(ord('@') + index + 1), next_row)
logger.debug('Using cell: {}'.format(cell_name))
ws.add_image(img, cell_name)
except Exception as exc:
logger.error('Error with image processing. Entering default value')
logger.debug('Details: {}'.format(str(exc)))
ws.cell(row=next_row, column=index + 1, value=value.replace('imagesrc:', ''))
else:
ws.cell(row=next_row, column=index + 1, value=value)
else:
# Existing file with previous data not found. Assumed to be first time scraping
logger.debug('New file created')
wb = Workbook()
logger.debug('Creating new sheet')
ws = wb.active
ws.title = self.get_keyword()
for index, (key, value) in enumerate(self.output.items()):
# Filling column titles
ws.cell(row=1, column=index + 1, value=key)
# Filling first row
if 'imagesrc:' in value:
try:
logger.debug('Processing image')
logger.debug('Changing cell dimensions')
ws.row_dimensions[2].height = 70
ws.column_dimensions[chr(ord('@') + index + 1)].width = 20
img = openpyxl.drawing.image.Image(self.process_image(value))
cell_name = '{0}{1}'.format(chr(ord('@') + index + 1), 2)
logger.debug('Using cell: {}'.format(cell_name))
ws.add_image(img, cell_name)
except Exception as exc:
logger.error('Error with image processing. Entering default value')
logger.debug('Details: {}'.format(str(exc)))
ws.cell(row=2, column=index + 1, value=value.replace('imagesrc:', ''))
else:
ws.cell(row=2, column=index + 1, value=value)
logger.debug('Saving entry to: {0}'.format(self.outputpath))
wb.save(self.outputpath)
class TrustpilotScraper(ScraperBase):
def __int__(self, url, outputpath, driver):
super(TrustpilotScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'trustpilot'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(2)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
logo_div = prod_soup.find('div', {'class': 'profile-image_imageWrapper__kDdWe'})
logo_image = 'imagesrc:' + logo_div.find_all('source')[1]['srcset'].split(',')[-1].split()[0].strip()
self.logger.debug('Logo image: {}'.format(logo_image))
self.output['Logo Image'] = logo_image
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Logo image'] = 'N/A'
main_div = prod_soup.find('div', {'id': 'business-unit-title'})
try:
name = main_div.find('h1').text.replace('Reviews', '').strip()
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
'''
tot_rew = main_div.find('p', {'class': 'typography_body-l__KUYFJ'
' typography_appearance-default__AAY17'}).text.split()[0].strip()
'''
tot_rew = prod_soup.find('div', {'class': 'styles_mainContent__nFxAv'})\
.find('div', {'class': 'paper_paper__1PY90 paper_outline__lwsUX card_card__lQWDv'
' styles_reviewsOverview__mVIJQ'}).find_all('p')[0].text.split()[0].strip()
self.logger.debug('Total Reviews: {}'.format(tot_rew))
self.output['Total Reviews'] = tot_rew
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Total Reviews'] = 'N/A'
try:
stars = main_div.find('div', {'class': 'styles_container__OaEK8'}).find('p').text
self.logger.debug('Main star rating: {}'.format(stars))
self.output['Main star rating'] = stars
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Main star rating'] = 'N/A'
star_range = None
star_image = None
try:
rating_box = main_div.find('div', {'class': 'star-rating_starRating__4rrcf star-rating_medium__iN6Ty'})
star_range = rating_box.find('img')['alt']
self.logger.debug('Star range: {}'.format(rating_box.find('img')['alt']))
self.output['Star range'] = star_range
star_image = 'imagesrc:' + rating_box.find('img')['src']
self.logger.debug('Star Image: {}'.format(star_image))
self.output['Star Image'] = star_image
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
if star_range is None:
self.output['star_range'] = 'N/A'
if star_image is None:
self.output['star_image'] = 'N/A'
try:
verified = 'Yes' if main_div.find('button', {'class': 'styles_verificationLabel__kukuk'}) is not None else 'No'
self.logger.debug('Verified: {}'.format(verified))
self.output['Verified'] = verified
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Main star rating'] = 'N/A'
self.save_into_file()
class FacebookScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(FacebookScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'facebook'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(2)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
name = prod_soup.find('h1').text
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
likes = ScraperBase.process_number(prod_soup.find('span', {'class': 'd2edcug0 hpfvmrgz qv66sw1b c1et5uql lr9zc1uh jq4qci2q'
' a3bd9o3v b1v8xokw oo9gr5id'}).text.replace(' people', ''))
self.logger.debug('Likes: {}'.format(likes))
self.output['Likes'] = likes
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Likes'] = 'N/A'
follows1 = None
follows2 = None
try:
follows1 = ScraperBase.process_number(prod_soup.find_all('span', {'class': 'd2edcug0 hpfvmrgz qv66sw1b c1et5uql'
' lr9zc1uh jq4qci2q a3bd9o3v b1v8xokw'
' oo9gr5id'})[2].text
.replace('Follows: ', '').replace(' people follow this', ''))
self.logger.debug('Follows 1: {}'.format(follows1))
self.output['Follows1'] = follows1
follows2 = ScraperBase.process_number(prod_soup.find('a', {'class': 'oajrlxb2 g5ia77u1 qu0x051f esr5mh6w e9989ue4 r7d6kgcz rq0escxv'
' nhd2j8a9 nc684nl6 p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab'
' hcukyx3x jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso i1ao9s8h'
' esuyzwwr f1sip0of lzcic4wl gpro0wi8 m9osqain lrazzd5p'}).text\
.replace('Follows: ', '').replace(' people follow this', ''))
self.logger.debug('Follows 2: {}'.format(follows2))
self.output['Follows1'] = follows2
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
if follows1 is None:
self.output['Follows1'] = 'N/A'
if follows2 is None:
self.output['Follows2'] = 'N/A'
self.save_into_file()
class TwitterScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(TwitterScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'twitter'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(5)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
name = prod_soup.find_all('span', {'class': 'css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0'})[10].text
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
followers = ScraperBase.process_number(prod_soup.find('span', string='Followers').find_previous().text)
self.logger.debug('Followers: {}'.format(followers))
self.output['Followers'] = followers
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Followers'] = 'N/A'
self.save_into_file()
class LinkedinScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(LinkedinScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'linkedin'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(5)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
name = prod_soup.find('h1', {'class': 'top-card-layout__title font-sans text-lg papabear:text-xl'
' font-bold leading-open text-color-text mb-0'}).text.strip()
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
followers = ScraperBase.process_number(prod_soup.find('h3', {'class': 'top-card-layout__first-subline font-sans text-md leading-open'
' text-color-text-low-emphasis'}).text.strip().split()[2])
self.logger.debug('Followers: {}'.format(followers))
self.output['Followers'] = followers
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Followers'] = 'N/A'
self.save_into_file()
class YoutubeScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(YoutubeScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'youtube'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(2)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
name = prod_soup.find('yt-formatted-string', {'id': 'text', 'class': 'style-scope ytd-channel-name'}).text
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
subs = prod_soup.find('yt-formatted-string', {'id': 'subscriber-count'}).text\
.replace(' subscribers', '')
subscribers = ScraperBase.process_number(subs)
self.logger.debug('Subscribers: {}'.format(subscribers))
self.output['Subscribers'] = subscribers
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Subscribers'] = 'N/A'
self.save_into_file()
class TiktokScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(TiktokScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'tiktok'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(5)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
name = prod_soup.find('h2', {'class': 'tiktok-b7g450-H2ShareTitle ekmpd5l5'}).text
self.logger.debug('Name: {}'.format(name))
self.output['Name'] = name
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Name'] = 'N/A'
try:
followers = ScraperBase.process_number(prod_soup.find_all('div',
{'class': 'tiktok-xeexlu-DivNumber e1457k4r1'})[1]
.find('strong').text)
self.logger.debug('Followers: {}'.format(followers))
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Followers'] = 'N/A'
try:
likes = ScraperBase.process_number(prod_soup.find_all('div', {'class': 'tiktok-xeexlu-DivNumber e1457k4r1'})
[2].find('strong').text)
self.logger.debug('Likes: {}'.format(likes))
self.output['Likes'] = likes
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Likes'] = 'N/A'
self.save_into_file()
class InstagramScraper(ScraperBase):
def __init__(self, url, outputpath, driver):
super(InstagramScraper, self).__init__(url, outputpath, driver)
@staticmethod
def get_keyword():
return 'instagram'
def get_data(self):
self.logger.info('Starting scraping process')
self.driver.get(self.url)
time.sleep(5)
prod_soup = BeautifulSoup(self.driver.page_source, 'html.parser')
try:
title = prod_soup.find('h2').text
self.logger.debug('Title: {}'.format(title))
self.output['Title'] = title
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Title'] = 'N/A'
try:
followers = ScraperBase.process_number(prod_soup.find_all('li', {'class': '_aa_5'})[1]
.find('span')['title'])
self.logger.debug('Followers: {}'.format(followers))
except Exception as exc:
self.logger.error('Could not get data')
self.logger.debug('Details: {}'.format(str(exc)))
self.output['Followers'] = 'N/A'
self.save_into_file()