-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (152 loc) · 6.2 KB
/
main.py
File metadata and controls
177 lines (152 loc) · 6.2 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
from selenium import webdriver
from Levenshtein import distance
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import json
import math
import re
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()
# setBinary to brave browser
options.binary_location = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
# disable js
driver = webdriver.Chrome(options=options)
def group_similar_links(links, threshold):
groups = []
for link in links:
added = False
for group in groups:
if distance(link, group[0]) < threshold:
group.append(link)
added = True
break
if not added:
groups.append([link])
return groups
data_amount = 0
num_pages = 5 # set the number of pages to scrape here
num_pages = math.floor(num_pages)
for num in range(1, num_pages):
url = 'https://www.amazon.in/s?k=cosmetic&ref=nb_sb_noss'
# gets links of products on the page and returns a list of links
def get_product_links(url):
driver.get(url)
links = []
data_span_xpath = '/html/body/div[1]/div[2]/div[1]/div[1]/div/span[1]'
data_span = driver.find_element(By.XPATH, data_span_xpath)
data_divs = data_span.find_elements(
By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[1]/div/span[1]/div[1]/div')
for divs in data_divs:
data_index = int(divs.get_attribute('data-index'))
if not (data_index >= 2 and data_index <= 55):
continue
c = data_index + 1
try:
# get the anchor tag
anchor = divs.find_element(
By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[1]/div/span[1]/div[1]/div[' + str(c) + ']/div/div/div/div/div[1]/span/a')
links.append(anchor.get_attribute('href'))
except NoSuchElementException:
print("No such element on index: " + str(c) + " skipping...")
continue
return links
my_links = get_product_links(url)
print(my_links) # print the links for testing purposes
def get_details(url):
wait = WebDriverWait(driver, 0.5)
action = ActionChains(driver)
driver.get(url)
# time.sleep(1)
data_dict = {}
print('getting title...')
try:
title = wait.until(ec.presence_of_element_located(
(By.XPATH, '//*[@id="productTitle"]'))).text
except TimeoutException:
title = 'N/A'
print('getting image...')
try:
main_img = wait.until(ec.presence_of_element_located(
(By.XPATH, '/html/body/div[2]/div[2]/div[5]/div[4]/div[3]/div[1]/div[1]/div/div/div[2]/div[1]/div[1]/ul/li[1]/span/span/div/img'))).get_attribute('src')
except TimeoutException:
main_img = 'N/A'
print('getting description...')
#images for amazon
images = []
try:
alt_images = wait.until(ec.presence_of_all_elements_located(
(By.CLASS_NAME, "imageThumbnail")))
except:
alt_images = wait.until(ec.presence_of_all_elements_located(
(By.CLASS_NAME, "thumbTypeimage")))
for image in alt_images:
image.click()
image.click()
dynamic_image = wait.until(ec.presence_of_all_elements_located(
(By.CLASS_NAME, "a-dynamic-image")))
for img in dynamic_image:
images.append(img.get_attribute('src'))
final_imgs = []
for image in images:
image = re.sub(r',', '', image)
#only take images that have m.media-amazon.com/images/I
if 'm.media-amazon.com/images/I' in image:
final_imgs.append(image)
print(len(final_imgs))
disticnt_imgs = list(set(final_imgs))
print(len(disticnt_imgs))
# images for amazon
try:
##feature-bullets > ul css selector
description = wait.until(ec.presence_of_element_located(
(By.XPATH, "//div[@id='feature-bullets']//ul"))).text
except TimeoutException:
description = 'N/A'
print('getting list price...')
try:
list_price = wait.until(ec.presence_of_element_located(
(By.CLASS_NAME, 'a-price'))).text
except TimeoutException:
list_price = 'N/A'
print('getting price...')
#class="a-price"
try:
price = wait.until(ec.presence_of_element_located(
(By.CLASS_NAME, 'a-price-whole'))).text
except TimeoutException:
price = 'N/A'
print('getting discount...')
try:
discount = wait.until(ec.presence_of_element_located(
(By.CLASS_NAME, 'savingsPercentage'))).text
except TimeoutException:
discount = 'N/A'
print('getting product details...')
#id="detailBullets_feature_div"
try:
prod_det = wait.until(ec.presence_of_element_located(
(By.XPATH, "//div[@id='detailBulletsWrapper_feature_div']//ul"))).text
except TimeoutException:
prod_det = 'N/A'
data_dict = {
'url': url,
'title': title,
'main_img': main_img,
'images': group_similar_links(disticnt_imgs, 10)[0],
'description': description,
'list_price': list_price,
'price': price,
'discount': discount,
'details': prod_det
}
return data_dict
for link in my_links:
data = get_details(link)
with open('cosmetic.json', 'a') as f:
json.dump(data, f, indent=4)
f.write(',')
driver.quit()