Repository navigation
Expand file tree
/
Copy pathAuthorProfileConfig.py
More file actions
608 lines (523 loc) · 16.8 KB
/
Copy pathAuthorProfileConfig.py
File metadata and controls
608 lines (523 loc) · 16.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
from selenium.common.exceptions import NoSuchElementException
class AuthorConfiguration:
"""
get_author_name():
Get the name of author
get_posted_date():
Get the date a review was posted
get_title():
Get the title of review
get_ratings():
Get ratings for a review
get_reviews():
Get review text
get_helpful_count():
Get the count of helpful votes for a review
is_verified_purchase():
Check whether review is described verified_purchased or not
get_product_url():
Get the url of the sub product.
get_profile_image():
Get the profile image of the author.
get_cover_image():
Get the cover image of the author.
get_category():
Get category of the product.
getPageContent():
Retrieve the info about brand and rank.
getReviewer():
Gives the name of the reviewer.
getRatings():
Gives the ratings associated with the review.
getDate():
Gives the date of review posted.
isVerifiedPurchase():
Check if the product is verified_purchased or not.
getReview():
Get the reviews.
peopleFindHelpful():
Get count of people find the review helpful.
getReviewTitle():
Get the title of the review.
getProductName():
Get the name of the product.
getAuthorProfile():
Get the url of the author to scrape.
get_summary_table():
Gives table with brand name.
get_brand():
If brand is not present in get_summary_table(), then this function will check.
get_extra_info():
Gives table with rank of the product.
get_rank():
If rank is not present in get_extra_info(), then this function will check.
"""
def get_author_name(self, driver):
"""
Get the name of the author.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
name: string
name of the author
"""
path = '#customer-profile-name-header > div.a-row.a-spacing-none.name-container > span'
try:
name = driver.find_element_by_css_selector(path).text
return name
except NoSuchElementException:
return ""
def get_posted_date(self, driver):
"""
Get the date the review was posted.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
date: string
date the review was posted
"""
# path = 'div.a-profile-content > span.a-profile-descriptor' # if no more reviews
path = '//span[@data-hook="review-date"]'
try:
# date = driver.find_element_by_css_selector(path).text
date = driver.find_element_by_xpath(path).text
date = date.split()[-3:]
date = ' '.join(map(str, date))
return date
except NoSuchElementException:
print("No date found...")
return ""
def get_title(self, driver):
"""
Title of the review.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
title: string
title related to a review
"""
# path = '#profile-at-card-container div.a-section.a-spacing-none span '
path = '//a[@data-hook="review-title"]'
try:
# title = driver.find_element_by_css_selector(path).text
title = driver.find_element_by_xpath(path).text
return title
except NoSuchElementException:
print("No title found...")
return ""
def get_ratings(self, driver):
"""
Ratings for a review.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
rating: string
ratings related to a review
"""
path = '.a-link-normal'
try:
rating = driver.find_element_by_css_selector(path).get_attribute('title')
rating = rating.split()[0]
return rating
except NoSuchElementException:
return ""
def get_reviews(self, driver):
"""
Get the review.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
review: string
review posted
"""
path = '//span[@data-hook="review-body"]'
try:
review = driver.find_element_by_xpath(path).text
return review
except NoSuchElementException:
return ""
def get_helpful_count(self, driver):
"""
Number of people found a review helpful.
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
count: string
number of people found helpful
"""
path = 'span.cr-vote > div.a-row.a-spacing-small > span'
try:
text = driver.find_element_by_css_selector(path).get_attribute('textContent')
count = text.split()[0]
return count
except NoSuchElementException:
return 0
def is_verified_purchase(self, driver):
"""
Check review is verified_purchased or not
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
check: string
whether review is labeled Verified_purchased or not
"""
path = '//span[@data-hook="avp-badge"]'
try:
check = driver.find_element_by_xpath(path).text
return "yes"
except NoSuchElementException:
return "no"
def get_product_url(self, driver):
"""
get product url
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
url: string
url of the product
"""
path = '//a[@data-hook="product-link"]'
try:
url = driver.find_element_by_xpath(path).get_attribute('href')
return url
except NoSuchElementException:
return ""
def get_profile_image(self, driver):
"""
retrieve path of profile picture of the reviewer
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
img: string
source path of image
"""
path = '//*[@id="avatar-image"]'
try:
img = driver.find_element_by_xpath(path).get_attribute('src')
return img
except NoSuchElementException:
return ""
def get_cover_image(self, driver):
"""
retrieve path of cover picture of the reviewer
Parameters
----------
driver : selenium web driver object
object of webdriver.
Returns
-------
img: string
source path of image
"""
path = '//*[@id="cover-image-with-cropping"]'
try:
img = driver.find_element_by_xpath(path).get_attribute('src')
return img
except NoSuchElementException:
return ""
def get_category(self, driver):
path = '//*[@id="wayfinding-breadcrumbs_feature_div"]/ul'
try:
li_list = driver.find_elements_by_xpath(path)
return li_list
except NoSuchElementException:
return False
"""
*******************************************************************************
Functions for product scraping (level 3)
*******************************************************************************
"""
def getPageContent(self, driver):
"""
This function finds and return the content of page where all reviews are located.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
list
list of elements attached to the given page
includes inforation associated with reviews, hyperlinks - See more reviews (from Canada), Next page button
"""
path = '//div[@id="cm_cr-review_list"]'
page_content = driver.find_elements_by_xpath(path)
return page_content
def getReviewer(self, driver, id):
"""
This function returns name of a reviewer.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
string
name of reviewer
"""
path = '#customer_review-' + id + ' div.a-profile-content > span'
try:
reviewer = driver.find_element_by_css_selector(path).get_attribute('textContent').split('\n')
return reviewer[0]
except NoSuchElementException:
return False
def getRatings(self, driver, id):
"""
This function returns ratings given by a reviewer.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
float(or empty if not found)
ratings given by reviewer
"""
path = '//*[@id="customer_review-' + id + '"]/div[2]/a[1]'
try:
ratings = driver.find_element_by_xpath(path).get_attribute('title')
ratings = float(ratings[0])
except NoSuchElementException:
return ""
return ratings
def getDate(self, driver, id):
"""
This function returns date at which a review was posted by a reviewer
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
string
date a review was posted
"""
path = '//*[@id="customer_review-' + id + '"]//span[@data-hook="review-date"]'
message = str(driver.find_element_by_xpath(path).get_attribute('textContent'))
country = message.split()[2]
if country.lower() == 'canada':
message = message.split()[-3:]
date = ' '.join(map(str, message))
return date, True
else:
print('not canada')
return message, False
def isVerifiedPurchase(self, driver, id):
"""
This function checks whether a product is labeled as Verified Purchase or not.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
bool
True if product is labeled Verified Purchase else False
"""
path = '//*[@id="customer_review-' + id + '"]/div[3]/span/a/span'
try:
isverified = driver.find_element_by_xpath(path).text
return True
except NoSuchElementException:
return False
def getReview(self, driver, id):
"""
This function collects the review posted by a reviewer.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
string
review given by a reviewer
"""
path = '//*[@id="customer_review-' + id + '"]/div[4]/span'
return driver.find_element_by_xpath(path).text
def peopleFindHelpful(self, driver, id):
"""
This function checks the count for number of people who found the review helpful.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
string
number of people found helpful, if none returns empty string
"""
path = '#customer_review-' + id + ' span.cr-vote > div.a-row.a-spacing-small > span'
try:
text = driver.find_element_by_css_selector(path).get_attribute('textContent')
number = text.split()[0]
return number
except NoSuchElementException:
return 0
def getReviewTitle(self, driver, id):
"""
This function gets the review title given by reviewer.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
string
review title given by reviewer
"""
path = '//*[@id="customer_review-' + id + '"]/div[2]/a[2]/span'
try:
review = driver.find_element_by_xpath(path).text
return review
except NoSuchElementException:
return ""
def getProductName(self, driver):
"""
This function gets the name of the product.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
string
review name of product
"""
path = '//*[@id="productTitle"]'
try:
name = driver.find_element_by_xpath(path).text
return name
except NoSuchElementException:
return None
def getAuthorProfile(self, driver, id):
"""
This function gets the author url.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
id : int
unique id that corresponds to each unique review
Returns
-------
profile: string
author profile url
"""
path = '//*[@id="customer_review-' + id + '"]//div[@data-hook="genome-widget"]/a'
try:
profile = driver.find_element_by_xpath(path).get_attribute('href')
return profile
except NoSuchElementException:
return ""
def get_summary_table(self, driver):
"""
This function gets the brand of the product.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
table: list of web elements if present or None
It will return all the content of table
"""
path = '//*[@id="productDetails_techSpec_section_1"]//tbody/tr'
try:
table = driver.find_elements_by_xpath(path)
return table
except NoSuchElementException:
return None
def get_brand(self, driver):
"""
This function get the brand name
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
table: list of web elements if present or None
It will return all the content of table
"""
path = '//*[@id="detailBullets_feature_div"]//ul/li'
try:
table = driver.find_elements_by_xpath(path)
return table
except NoSuchElementException:
return None
def get_extra_info(self, driver):
"""
This function gets the table containing rank of the product
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
table: list of web elements if present or None
It will return all the content of table
"""
path = '//*[@id="productDetails_detailBullets_sections1"]//tbody/tr'
try:
table = driver.find_elements_by_xpath(path)
return table
except NoSuchElementException:
return None
def get_rank(self, driver):
"""
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
rank: string if present or None
It will return the rank of a product
"""
path = '//*[@id="SalesRank"]'
try:
rank = driver.find_element_by_xpath(path).text
return rank
except NoSuchElementException:
return None