-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
586 lines (509 loc) · 20.1 KB
/
test.py
File metadata and controls
586 lines (509 loc) · 20.1 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
# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
# from seleniumwire import webdriver # Install using: pip install selenium-wire
# List of user agents
# user_agents = [
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
# "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
# Add more user agents
# ]
# Randomly select a user agent
# import random
# user_agent = random.choice(user_agents)
# # Configure Selenium Wire options
# options = {
# 'headers': {
# 'User-Agent': user_agent,
# 'Accept-Language': 'en-US,en;q=0.9',
# # Add more headers if needed
# }
# }
# # Set up Selenium Wire driver
# driver = webdriver.Chrome(seleniumwire_options=options)
# driver.get("www.youtube.com")
# string = "Hello, Wrld!"
# substring = "World"
# result = string.replace(substring, "")
# print(result) # Output: "Hello, !"
# import pandas as pd
# file_path = "AmazonCategories_Cleaned_Final2.csv" # Update with your actual path
# # Read the CSV file with semicolon as the delimiter
# df = pd.read_csv(file_path, delimiter=";", encoding="utf-8")
# # Print column names to verify
# print("Column names in CSV:", df.columns)
# # Remove duplicates based on "Subcategory 1"
# df_deduplicated = df.drop_duplicates(subset=["Mai+Category", "Subcategory+1"], keep="first")
# # Save cleaned data
# output_path = "AmazonCategories_Cleaned_Final.csv"
# df_deduplicated.to_csv(output_path, index=False, sep=";") # Keep semicolon delimiter
# print(f"Duplicates removed. Cleaned data saved to {output_path}.")
# import re
# import time
# from datetime import datetime
# date_pattern = r"\b(January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4}\b"
# reviewTimes = 'Reviewed in the United States on February 7, 2025'
# reviewTimes = re.search(date_pattern, reviewTimes)
# reviewTiming = datetime.strptime(reviewTimes, "%B %d, %Y")
# print("Review Time :", reviewTiming)
# reviewTime = int(reviewTiming.timestamp())
# import pandas as pd
# import re
# file_path = "AmazonCategories_Final.csv" # Update with your actual path
# # Read the CSV file with semicolon delimiter
# df = pd.read_csv(file_path, delimiter=";", encoding="utf-8")
# # Function to clean text: replace spaces with '+' and remove special characters
# def clean_text(text):
# text = text.replace(" ", "+") # Replace spaces with '+'
# text = re.sub(r"[^a-zA-Z0-9+]", "", text) # Remove special characters
# return text
# # Apply cleaning to column names
# df.columns = [clean_text(col) for col in df.columns]
# # Apply cleaning to all column values
# df = df.applymap(lambda x: clean_text(str(x)) if isinstance(x, str) else x)
# # # Remove duplicates based on "Subcategory+1"
# # df_deduplicated = df.drop_duplicates(subset="Mai+Category", keep="first")
# # Save cleaned data
# output_file = "AmazonCategories_Cleaned_Final2.csv"
# df.to_csv(output_file, index=False, sep=";") # Keep semicolon delimiter
# print(f"Duplicates removed. Cleaned data saved to {output_file}.")
import requests as req
import pandas as pd
import requests
import time
import re
from bs4 import BeautifulSoup as bsp
from selenium import webdriver
from seleniumwire import webdriver # blinker == 1.7.0
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from fake_useragent import UserAgent
from googlesearch import search
from datetime import datetime
from collections import defaultdict
maxDate = "June 1, 2023"
maxDateT = datetime.strptime(maxDate, "%B %d, %Y")
base = 'https://www.amazon.com'
number_pattern = r"\d+"
date_pattern = r"\b(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4}\b"
options = {
'headers' : {
"User-Agent": UserAgent().random
}
}
agent = {
"main_category" : "",
"title" : "",
"average_rating" : 0.0,
"rating_number" : 0,
"features":[],
"description":[],
"price" : "",
"images":[],
"videos":[],
"store" : "",
"categories" : "",
"details":{},
"parent_asin" : "",
"bought_together": [],
"link" : "",
"date" : ""
}
comment = {
"rating" : 0.0,
"title" : "",
"text" : "",
"images" : "",
"asin" : "",
"parent_asin" : "",
"user_id" : "",
"timestamp" : "",
"verified_purchase" : 0,
"helpful_vote" : "",
"link" : ""
}
link = "http://amazon.com/New-Balance-Womens-Sneaker-Nimbus/dp/B093QHV57C/ref=sr_1_8?_encoding=UTF8&sr=8-8"
print("\n\n\n\n\n\n-------------------------------------------------------------------Product-------------------------------------------------------------------")
# Extracting Links
link = row['Products']
print(link)
agent["link"] = link
# Extracting Categories
category = row['Categories']
print("Category :", category)
agent["main_category"] = category
driver = webdriver.Chrome(seleniumwire_options = options)
driver.get(link)
time.sleep(10)
Previous_Height = driver.execute_script("return document.body.scrollHeight")
print(f"Height : {Previous_Height}" )
while True:
# It will scroll to right above the footer of the page then scoll to the top then back to the bottom untill there is no new items being loaded
driver.execute_script("window.scrollTo(0, document.body.scrollHeight - 1500);")
time.sleep(5)
driver.execute_script("window.scrollTo(0, 0);")
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight - 1500);")
time.sleep(5)
New_Height = driver.execute_script("return document.body.scrollHeight")
if New_Height == Previous_Height:
break
Previous_Height = New_Height
# if there is a popup press the close button
try:
driver.find_element_by_class_name('modal-close').click()
except:
pass
html = driver.page_source
driver.quit()
soup = bsp(html, 'lxml')
# extracting details
try:
detailsBoard = soup.find('div', class_ = 'a-expander-content a-expander-section-content a-section-expander-inner').find('table', class_ = 'a-keyvalue prodDetTable').find_all('tr')
except:
try:
detailsBoard = soup.find('div', id = 'detailBullets_feature_div').find_all('li')
except:
try:
detailsBoard = soup.find('table', id = 'productDetails_detailBullets_sections1').find_all('tr')
except:
try:
detailsBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-vertical a-spacing-none detail-bullet-list').find_all('tr')
except:
detailsBoard = []
details = {}
if detailsBoard:
for detailBoard in detailsBoard:
detailingH = detailBoard.find('th').text.strip() if detailBoard.find('th') else None
detailingA = detailBoard.find('td').text.strip() if detailBoard.find('td') else None
if detailingH and detailingA:
detailed = {detailingH: detailingA}
details |= detailed
else:
details = {}
print("Details :", details)
agent["details"] = details
date = ""
try:
for value in reversed(list(details.values())):
if isinstance(value, str):
date = re.search(date_pattern, value)
print("Date Found", date)
if date:
date = date.group(0)
dateT = datetime.strptime(date, "%B %d, %Y")
if dateT > maxDateT:
print(f"{date} is after {maxDateT}")
elif dateT < maxDateT:
print(f"{date} is before {maxDateT}")
else:
print(f"{date} is the same as {maxDateT}")
break
except:
print("Date not found")
date = ""
print("Date :", date)
agent["date"] = date
# extracting Title
try:
titleBoard = soup.find('span', id = 'productTitle')
title = titleBoard.text.strip()
except:
title = ""
print("Title :", title)
agent["title"] = title
# extracting ratings
try:
ratingBoard = soup.find('div', id = 'averageCustomerReviews')
# extracting average rating
avgRatingBoard = ratingBoard.find('span', class_ = 'a-size-base a-color-base')
avgRating = avgRatingBoard.text.strip()
# extracting rating number
ratingNoBoard = ratingBoard.find('span', id = 'acrCustomerReviewText')
ratingNo = ratingNoBoard.text.strip()
junk = " ratings"
ratingNo = ratingNo.replace(junk, "")
except:
avgRating = ""
ratingNo = ""
print("Average Rating :", avgRating)
agent["average_rating"] = avgRating
print("Rating :", ratingNo)
agent["rating_number"] = ratingNo
# extracting price
try:
priceBoard = soup.find('span', class_ = 'a-offscreen')
if not priceBoard:
# priceBoard = soup.find('div', class_ = 'a-section a-spacing-none aok-align-center aok-relative').find('span', class_ = 'aok-offscreen')
priceBoard = soup.find('span', class_ = 'a-price-whole')
priceBoard2 = soup.find('span', class_ = 'a-price-fraction')
price2 = priceBoard2.text.strip()
price = priceBoard.text.strip()
price = price + price2
else:
price = priceBoard.text.strip()
except:
price = ""
print("Price :", price)
agent["price"] = price
# exracting media
try:
mediaBoard = soup.find('div', id = 'altImages')
if not mediaBoard:
mediaBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-micro regularAltImageViewLayout')
if not mediaBoard:
mediaBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-micro gridAltImageViewLayoutIn1x7')
if not mediaBoard:
mediaBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-extra-large regularAltImageViewLayout')
if not mediaBoard:
mediaBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-button-list a-declarative a-button-toggle-group a-vertical a-spacing-top-extra-large regularAltImageViewLayout')
media = mediaBoard.find_all('img')
images = [img['src'] for img in media]
videos = [images[-1]]
images = images[0:-1]
except:
images = []
vid = []
print("Images :", images)
agent["images"] = images
print("Videos :", videos)
agent["videos"] = videos
# extracting the store
try:
storeBoard = soup.find('tr', class_ = 'a-spacing-small po-brand').find('td', class_ = 'a-span9')
store = storeBoard.text.strip()
except:
try:
storeBoard = soup.find('a', id = 'bylineInfo').text.strip()
junk = "Visit the "
store = storeBoard.replace(junk, "")
except:
store = ""
print("Store :", store)
agent["store"] = store
# extracting features
try:
featuresBoard = soup.find('ul', class_ = 'a-unordered-list a-vertical a-spacing-mini').find_all('li')
features = [feature.text.strip() for feature in featuresBoard]
except:
features = []
print("Features :", features)
agent["features"] = features
# extracting asin
try:
asin = details['ASIN']
except:
asin = ""
print("ASIN :", asin)
agent["parent_asin"] = asin
# agent["date"] = date
# extracting description
try:
descriptionBoard = soup.find('table', class_ = 'a-normal a-spacing-micro')
if not descriptionBoard:
descriptionBoard = soup.find('ul', id = 'a-nostyle').find_all('div', class_ = 'a-fixed-left-grid-col a-col-right')
descriptions = descriptionBoard.find_all('td', class_ = 'a-span9')
description = [dec.text.strip() for dec in descriptions]
except:
description = []
print("Description :", description)
agent["description"] = description
# extracting bought together
try:
boughtTogetherBoard = soup.find('div', id = 'a-cardui _c3AtZ_new-thumbnail-box_1W9Ku _c3AtZ_two-item-thumbnail-box_7kF95')
if not boughtTogetherBoard:
boughtTogetherBoard = soup.find('div', class_ = 'a-cardui _p13n-desktop-sims-fbt_fbt-desktop_new-thumbnail-box__36bD3 _p13n-desktop-sims-fbt_fbt-desktop_two-item-thumbnail-box__jV2am')
if not boughtTogetherBoard:
boughtTogetherBoard = soup.find('div', class_ = 'a-cardui _p13n-desktop-sims-fbt_fbt-desktop_new-thumbnail-box__36bD3')
boughtTogetherS = boughtTogetherBoard.find_all('a', class_ = 'a-link-normal a-text-normal', href = True)
if not boughtTogetherS:
boughtTogetherS = boughtTogetherBoard.find_all('a', class_ = 'a-link-normal', href = True)
boughtTogether = [base + link['href'] for link in boughtTogetherS]
except:
boughtTogether = []
print("Bought Together :", boughtTogether)
agent["bought_together"] = boughtTogether
# extracting further categories
try:
categoriesBoard = soup.find_all('div', id= 'tp-inline-twister-dim-values-container')
categoriesBoardAh = categoriesBoard[-1].find_all('li')
categories = [categoriesBoardh.text.strip() for categoriesBoardh in categoriesBoardAh]
except:
categories = []
print("Categories :", categories)
agent["categories"] = categories
############################################################################################################################################################
print("\n\n\n-------------------------------------------------------------------Comments-------------------------------------------------------------------")
# extracting reviews
try:
reviewsUS = soup.find('ul', id = 'cm-cr-dp-review-list').find_all('li')
except:
reviewUS = []
try:
reviewsGlobal = soup.find('ul', id = 'cm-cr-global-review-list').find_all('li')
except:
reviewsGlobal = []
reviews = reviewsUS + reviewsGlobal
for review in reviews:
try:
# extracting rating
reviewRatingBoard = review.find('h5')
reviewRatingBoard = reviewRatingBoard.find_all('span')
try:
reviewRating = reviewRatingBoard[0].text.strip()
print("Rating Found")
reviewRating = reviewRating[0:3]
except:
reviewRating = ""
try:
for span in reversed(reviewRatingBoard): # iterate from the end
text = span.get_text(strip=True)
if text: # if text is not empty
reviewTitle = text
break
print("Title Found")
except:
reviewTitle = ""
except:
reviewRating = ""
reviewTitle = ""
print("Rating :", reviewRating)
comment["rating"] = reviewRating
# extracting title
print("Title :", reviewTitle)
comment["title"] = reviewTitle
# extracting Text
try:
reviewTextBoard = review.find('div', class_ = 'a-expander-content reviewText review-text-content a-expander-partial-collapse-content')
reviewText = reviewTextBoard.text.strip()
except:
reviewText = ""
print("Review :", reviewText)
comment["text"] = reviewText
# extracting images
try:
reviewImages = review.find('div', class_ = 'review-image-tile-section').find_all('img')
Images = [img['src'] for img in reviewImages]
except:
Images = []
print("Images :", Images)
comment["images"] = Images
# extracting ASIN
try:
reviewAsin = review.get('id')
except:
reviewAsin = ""
print("ASIN :", reviewAsin)
comment["asin"] = (reviewAsin)
# extracting parent ASIN
reviewParentAsin = asin
print("Parent ASIN :", reviewParentAsin)
comment["parent_asin"] = review
# eaxtracting username
try:
username = review.find('span', class_ = 'a-profile-name').text.strip()
except:
username = ""
print("Username :", username)
comment["user_id"] = username
# extracting date
try:
reviewTimeBoard = review.find('span', class_ = 'a-size-base a-color-secondary review-date')
reviewTimes = reviewTimeBoard.text
reviewTime = re.search(date_pattern, reviewTimes)
reviewTime = reviewTime.group(0)
print("Date Found")
except:
reviewTime = ""
print("Date :", reviewTime)
comment["timestamp"] = reviewTime
verified = False
if review.find('span', class_ = 'a-size-mini a-color-state a-text-bold'):
verified = True
print("Verified :", verified)
comment["verified_purchase"] = verified
# extracting helpfulness
try:
reviewHelpful = review.find('span', class_ = 'a-size-base a-color-tertiary cr-vote-text')
reviewHelpful = reviewHelpful.text
haha = "One"
helpful = 0
Helpfulness = re.search(number_pattern, reviewHelpful)
if Helpfulness:
helpful = int(Helpfulness.group())
elif haha in reviewHelpful:
helpful = 1
except:
helpful = 0
print("Helpful :", helpful)
comment["helpful_vote"] = helpful
# extracting link
comment["link"] = link
myMeta = pd.DataFrame(agent)
myRev = pd.DataFrame(comment)
if not me:
myMeta.to_csv(meta, mode='a', index = False)
myRev.to_csv(rev, mode='a', index = False)
me = True
else:
myMeta.to_csv(meta, mode='a', header=False, index = False)
myRev.to_csv(rev, mode='a', header=False, index = False)
print("|||||||||||||||||||||||||||| Data appended successfully with continued index. |||||||||||||||||||||||||||")
# print("\n\n\n\n\n\n-------------------------------------------------------------------Product-------------------------------------------------------------------")
# options = {
# 'headers' :{
# "User-Agent":UserAgent().random
# }
# }
# link = "https://www.amazon.com/HEX-BOTS-Crawler-Rechargeable-Control/dp/B0CSFPL6JN/ref=sr_1_2?_encoding=UTF8&sr=8-2"
# print(link)
# driver = webdriver.Chrome(seleniumwire_options = options)
# driver.get(link)
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# driver.execute_script("window.scrollTo(0, 0);")
# time.sleep(10)
# html = driver.page_source
# driver.quit()
# soup = bsp(html, 'lxml')
# feat = []
# img = []
# vid = []
# det = []
# # extracting details
# try:
# detailsBoard = soup.find('div', class_ = 'a-expander-content a-expander-section-content a-section-expander-inner').find('table', class_ = 'a-keyvalue prodDetTable').find_all('tr')
# except:
# try:
# detailsBoard = soup.find('div', id = 'detailBullets_feature_div').find_all('li')
# except:
# try:
# detailsBoard = soup.find('table', id = 'productDetails_detailBullets_sections1').find_all('tr')
# except:
# try:
# detailsBoard = soup.find('ul', class_ = 'a-unordered-list a-nostyle a-vertical a-spacing-none detail-bullet-list').find_all('tr')
# except:
# detailsBoard = []
# # find th for the dictionary subject and td for the disctionary details
# details = {}
# if detailsBoard:
# for detailBoard in detailsBoard:
# detailingH = detailBoard.find('th').text.strip() if detailBoard.find('th') else None
# detailingA = detailBoard.find('td').text.strip() if detailBoard.find('td') else None
# if detailingH and detailingA:
# detailed = {detailingH: detailingA}
# details |= detailed
# else:
# details = {}
# print("Details :", details)
# date_pattern = r"\b(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4}\b"
# try:
# for value in details.values():
# if isinstance(value, str):
# date = re.findall(date_pattern, value)
# if date:
# break
# except:
# date = ""
# print("Date :", date)