-
Notifications
You must be signed in to change notification settings - Fork 3
/
scrape_amazon.py
172 lines (165 loc) · 8 KB
/
scrape_amazon.py
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
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import MoveTargetOutOfBoundsException
import re
import time
def amazon(product, driver):
action = ActionChains(driver)
driver.get(product.url)
time.sleep(4)
# catch a list of bullet points as selenium element
try:
driver.execute_script('window.scrollTo(0, 500)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 700)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 900)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1000)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1200)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1350)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1500)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1600)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 1760)')
time.sleep(0.5)
driver.execute_script('window.scrollTo(0, 2130)')
time.sleep(1.5)
try:
description = driver.find_element_by_css_selector("#productDescription_feature_div")
except NoSuchElementException:
try:
description = driver.find_element_by_xpath("//*[@id='aplus']/div")
except NoSuchElementException:
description = "not found"
driver.execute_script("window.scrollTo(0, 0)")
try:
source_code = description.get_attribute("outerHTML")
except AttributeError:
source_code = description
product.description = source_code
bullet_points = driver.find_element_by_css_selector("#feature-bullets").find_elements_by_tag_name("li")
if not bullet_points:
for i in range(6):
product.append_variant(" ", "bullet")
else:
for bp in bullet_points:
product.append_variant(bp.text, "bullet")
except:
for i in range(6):
product.append_variant("not found", "bullet")
driver.execute_script("window.scrollTo(0, 0)")
product.sku = re.search(r'/dp/([A-Z][0-9])\w+', product.url).group(0)[4:]
try:
product.name = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div[5]/div[5]/div[4]/div[1]/div"))).text
except TimeoutException:
product.name = "not found"
time.sleep(1)
try:
product.main_image = driver.find_element_by_css_selector("#landingImage").get_attribute("src").split("_AC_")[0] + "_AC_SX650_.jpg"
except NoSuchElementException:
product.main_image = "not found"
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//*[@id='nav-global-location-slot']/span/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='GLUXCountryValue']"))).click()
time.sleep(5)
regions = driver.find_elements_by_xpath("//*[@id='a-popover-5']/div/div/ul/li")
for i in regions:
if "India" in i.text:
i.click()
break
else:
pass
try:
action.move_by_offset(250, 50).click().perform()
except MoveTargetOutOfBoundsException:
pass
time.sleep(3)
# base url to get variant itens
products = driver.find_elements_by_class_name("swatchAvailable")
base = product.url.split('/dp/')[0]
url_variants = []
product.append_variant(product.sku, "variant_asin")
for p in products:
url_variants.append(str(base) + p.get_attribute("data-dp-url"))
product.append_variant(p.get_attribute("data-dp-url").split("/ref")[0].split("/dp/")[1], "variant_asin")
try:
price = driver.find_element_by_css_selector("#price").text
product.append_variant(re.search(r"[0-9]\w*.[0-9]\w*", price).group(0).replace(',', '.'), "price")
except:
price = 0
product.append_variant(price, "price")
try:
shipping = driver.find_element_by_css_selector("#exports_desktop_qualifiedBuybox_tlc_feature_div").text
product.append_variant(re.search(r"[0-9]\w*.[0-9]\w*", shipping).group(0).replace(',', '.'), "shipping")
except:
shipping = 0
product.append_variant(shipping, "shipping")
try:
product.append_variant(
driver.find_element_by_xpath("//*[@id='variation_color_name']/div").text.replace("Color:", ""), "variant_names")
except:
pass
try:
product.append_variant(
driver.find_element_by_xpath("//*[@id='variation_color_name']/ul").find_elements_by_tag_name("img")[
0].get_attribute("src").split("_SS36_")[0] + "_AC_SX650_.jpg", "variant_images")
except:
pass
for u in range(len(url_variants)):
driver.get(url_variants[u])
try:
price = driver.find_element_by_css_selector("#price_inside_buybox").text
product.append_variant(re.search(r"[0-9]\w*.[0-9]\w*", price).group(0).replace(',', '.'), "price")
except NoSuchElementException:
product.append_variant(0, "price")
try:
# search and format the shipping price
shipping = driver.find_element_by_css_selector("#exports_desktop_qualifiedBuybox_tlc_feature_div").text
product.append_variant(re.search(r"[0-9]\w*.[0-9]\w*", shipping).group(0).replace(',', '.'), "shipping")
except NoSuchElementException:
# if dont find shipping price (in case of free shipping or don't ship) shipping value will be 0
product.append_variant(0, "shipping")
time.sleep(5)
driver.execute_script("window.scrollTo(0, 540)")
time.sleep(3)
images_preview = driver.find_elements_by_class_name("a-spacing-small item imageThumbnail a-declarative")
product.append_variant(driver.find_element_by_xpath("//*[@id='variation_color_name']/div").text.replace("Color:", ""), "variant_names")
for i in range(len(images_preview)):
action.move_to_element(images_preview[i]).perform()
try:
images = driver.find_elements_by_xpath("/html/body/div[2]/div[2]/div[5]/div[5]/div[3]/div/div[1]/div/div/div[1]/ul/li")
except NoSuchElementException:
images = driver.find_elements_by_xpath("//*[@id='altImages']/ul/li")
if not images:
for i in range(5):
product.append_variant(" ", "other_main")
else:
for i in range(len(images)):
try:
img = driver.find_element_by_xpath(f"/html/body/div[2]/div[2]/div[5]/div[5]/div[3]/div/div[1]/div/div/div[1]/ul/li[{i + 1}]/span/span/span/span/img").get_attribute("src")
if img == "https://images-na.ssl-images-amazon.com/images/G/30/HomeCustomProduct/360_icon_73x73v2._CB485971309_SS40_FMpng_RI_.png":
pass
else:
product.append_variant(img.split("_AC_")[0] + "_AC_SX650_.jpg", "other_main")
except NoSuchElementException:
pass
if not products:
for i in range(5):
product.append_variant(" ", "variant_images")
else:
for i in range(len(products)):
try:
product.append_variant(driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[5]/div[5]/div[4]/div[27]/div[1]/div/form/div/ul").find_elements_by_tag_name("img")[i + 1].get_attribute("src").split("_SS36_")[0] + "_AC_SX650_.jpg", "variant_images")
except NoSuchElementException:
product.append_variant(driver.find_element_by_xpath("//*[@id='variation_color_name']/ul").find_elements_by_tag_name("img")[i + 1].get_attribute("src").split("_SS36_")[0] + "_AC_SX650_.jpg", "variant_images")
product.amz = True
return product