Repository navigation
Expand file tree
/
Copy pathAmazonConfig.py
More file actions
467 lines (394 loc) · 13.9 KB
/
Copy pathAmazonConfig.py
File metadata and controls
467 lines (394 loc) · 13.9 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
'''
#################################################
@product: Guide Product Analysis
@filename: Amazon Config File (Web elements path(CSS / XPath))
@author: Raj Patel
@date: August 5, 2020
##################################################
'''
from selenium.common.exceptions import NoSuchElementException
from word2number import w2n
class AmazonConfig:
"""
Specifies web elements path to scrap data from.
...
Methods
-------
getPageContent()
This function finds and return the content of page where all reviews are located.
getPrice()
This function gets dthe price of the product.
getProductImageURL()
This function gets the url of product image.
getReviewer()
This function returns name of a reviewer.
getRatings()
This function returns ratings given by a reviewer.
getDate()
This function returns date at which a review was posted by a reviewer.
isVerifiedPurchase()
This function checks whether a product is labeled as Verified Purchase or not.
getReview()
This function collects the review posted by a reviewer.
peopleFindHelpful()
This function checks the count for number of people who found the review helpful.
getAvgRating()
This function collect the average rating of a product.
getReviewCount()
This function collect the total reviews for a product.
getReviewTitle():
This function gets the review title given by reviewer.
getProductName():
This function gets the name of the product.
getAuthorProfile():
This function gets the profile url of the author.
get_category():
This function gets the category of the product.
"""
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 getPrice(self, driver):
"""
This function gets the price of the product.
"""
try:
path = "//*[@id='price_inside_buybox']"
price = driver.find_element_by_xpath(path).text
price = price.split()[-1]
return price
except NoSuchElementException:
return ""
def getProductImageURL(self, driver):
"""
This function returns the url of a product image.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
string
url of a product image
"""
path = '//*[@id="landingImage"]'
try:
url = driver.find_element_by_xpath(path).get_attribute('src')
return url
except NoSuchElementException:
return ""
def getReviewer(self, driver, flag, unique_id):
"""
This function returns name of a reviewer.
Parameters
----------
flag
driver : selenium webdriver object
web driver of selenium
unique_id : int
unique unique_id that corresponds to each unique review
Returns
-------
string
name of reviewer
"""
if flag:
path = '#customer_review-' + unique_id + ' div.a-profile-content > span'
else:
path = '#customer_review_foreign-' + unique_id + ' div.a-profile-content > span'
# //*[@unique_id="customer_review_foreign-R3MS1MSTBAXCU6"]/div[1]/div/div[2]/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, unique_id, is_canada):
"""
This function returns ratings given by a reviewer.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
unique_id : int
unique unique_id that corresponds to each unique review
Returns
-------
float(or empty if not found)
ratings given by reviewer
"""
if is_canada:
path = '//*[@id="customer_review-'+unique_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
else:
path = '//*[@id="customer_review_foreign-'+unique_id+'"]/div[2]/i/span'
try:
ratings = driver.find_element_by_xpath(path).get_attribute('textContent')
# print(ratings)
ratings = float(ratings[0])
except NoSuchElementException:
return ""
return ratings
def getDate(self, driver, unique_id):
"""
This function returns date at which a review was posted by a reviewer
Parameters
----------
driver : selenium webdriver object
web driver of selenium
unique_id : int
unique unique_id that corresponds to each unique review
Returns
-------
string
date a review was posted
"""
try:
path = '//*[@id="customer_review-'+unique_id+'"]//span[@data-hook="review-date"]'
message = str(driver.find_element_by_xpath(path).get_attribute('textContent'))
country = message.split()[2].lower()
if country == 'canada':
message = message.split()[-3:]
date = ' '.join(map(str,message))
return date, country, True
except NoSuchElementException:
try:
path = '//*[@id="customer_review_foreign-'+unique_id+'"]//span[@data-hook="review-date"]'
message = str(driver.find_element_by_xpath(path).get_attribute('textContent'))
cnt = message.lower().split()[2:]
del cnt[-4:]
country = ' '.join(map(str, cnt))
message = message.split()[-3:]
date = ' '.join(map(str,message))
return date, country, False
except NoSuchElementException:
return "","", False
def isVerifiedPurchase(self, driver, id, is_canada):
"""
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
"""
if is_canada:
path = '//*[@id="customer_review-'+id+'"]/div[3]/span/a/span'
else:
path = '//*[@id="customer_review_foreign-'+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, is_canada):
"""
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
"""
if is_canada:
path = '//*[@id="customer_review-'+id+'"]/div[4]/span'
else:
path = '//*[@id="customer_review_foreign-'+id+'"]/div[4]/span'
return driver.find_element_by_xpath(path).text
def peopleFindHelpful(self, driver, id, is_canada):
"""
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
"""
if is_canada:
path = '#customer_review-'+id+' span.cr-vote > div.a-row.a-spacing-small > span'
else:
path = '//*[@id="customer_review_foreign-'+id+' span.cr-vote > div.a-row.a-spacing-small > span'
try:
text = driver.find_element_by_css_selector(path).get_attribute('textContent')
try:
number = int(text.split()[0])
return number
except ValueError:
number = w2n.word_to_num(text.split()[0])
return number
except NoSuchElementException:
return 0
def getAvgRating(self, driver):
"""
This function collect the average rating of a product.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
float
average rating of a product
"""
path = '//span[@data-hook="rating-out-of-text"]'
ratings = driver.find_element_by_xpath(path).text.split()
rating = float(ratings[0])
return rating
def getReviewCount(self, driver):
"""
This function collect the total reviews for a product.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
int
total reviews of a product
"""
path = '//div[@data-hook="total-review-count"]/span'
total_rating = driver.find_element_by_xpath(path).text.split()
total_count = total_rating[0]
return total_count
def getReviewTitle(self, driver, id, is_canada):
"""
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
"""
if is_canada:
path = '//*[@id="customer_review-'+id+'"]/div[2]/a[2]/span'
else:
path = '//*[@id="customer_review_foreign-'+id+'"]/div[2]/span[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
-------
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_category(self, driver):
"""
This function gets the category of the product.
Parameters
----------
driver : selenium webdriver object
web driver of selenium
Returns
-------
string
author profile url
"""
path = '//*[@id="wayfinding-breadcrumbs_feature_div"]/ul'
try:
li_list = driver.find_elements_by_xpath(path)
return li_list
except NoSuchElementException:
return ""
def get_summary_table(self, driver):
"""
This function gets the brand of the product.
"""
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):
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):
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):
path = '//*[@id="SalesRank"]'
try:
rank = driver.find_element_by_xpath(path).text
return rank
except NoSuchElementException:
return None