-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathamazon.py
62 lines (49 loc) · 2.11 KB
/
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
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
PATH = "C:\Program Files\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.amazon.in")
driver.maximize_window()
search_box = driver.find_element(By.ID,"twotabsearchtextbox")
search_box.clear()
search_box.send_keys("laptops")
driver.find_element(By.ID,"nav-search-submit-button").click()
laptops = driver.find_elements(By.XPATH,'//div[@data-component-type="s-search-result"]')
laptop_name = []
laptop_price = []
no_reviews = []
final_list = []
for laptop in laptops:
names =laptop.find_elements(By.XPATH,".//span[@class='a-size-medium a-color-base a-text-normal']")
for name in names:
laptop_name.append(name.text)
try:
if len(laptop.find_elements(By.XPATH,".//span[@class='a-price-whole']"))>0:
prices= laptop.find_elements(By.XPATH,".//span[@class='a-price-whole']")
for price in prices:
# print('the lenght is ===>',len(price.text))
laptop_price.append(price.text)
else:
laptop_price.append("0")
except:
pass
# reviews = laptop.find_elements(By.XPATH,".//span[@class='a-size-base s-underline-text']")
try:
if len(laptop.find_elements(By.XPATH,".//span[@class='a-size-base s-underline-text']"))>0:
reviews = laptop.find_elements(By.XPATH,".//span[@class='a-size-base s-underline-text']")
for review in reviews:
# print('the length is===>', len(review.text), review.text)
no_reviews.append(review.text)
else:
no_reviews.append("0")
except:
pass
print('no of laptops==>',len(laptop_name))
print('no of prices==>',len(laptop_price))
print('no of reviews==>',len(no_reviews))
import pandas as pd
df = pd.DataFrame(zip(laptop_name,laptop_price,no_reviews),columns=['laptop_name','laptop_price','no_reviews'])
df.to_excel(r"C:\\Users\\ashwi\\OneDrive\Desktop\\Laptop Recommender\scapdata\\laptops.xlsx",index=False)
driver.quit()