-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaraz_assignment1.py
More file actions
51 lines (37 loc) · 1.36 KB
/
daraz_assignment1.py
File metadata and controls
51 lines (37 loc) · 1.36 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
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Initialize the WebDriver
driver = webdriver.Chrome()
# Maximize the browser window
driver.maximize_window()
# List to store product links
link_list = []
# Loop through the pages
for page in range(1, 3):
# Navigate to the page
driver.get(f"https://www.daraz.com.bd/men-muslimin-shirts/?page={page}")
time.sleep(3) # Wait for the page to load
# Determine the number of items on the current page
if page == 1:
num_items = 40
else:
num_items = 27
# Loop through the items on the current page
for i in range(1, num_items + 1):
try:
# Construct the XPath for the item
xpath = f'//*[@id="root"]/div/div[2]/div[1]/div/div[1]/div[2]/div[{i}]/div/div/div[2]/div[2]/a'
# Find the element and get the href attribute
link = driver.find_element(By.XPATH, xpath).get_attribute('href')
# Append the link to the list
link_list.append(link)
except Exception as e:
print(f"Error on page {page}, item {i}: {e}")
# Print the collected links
for link in link_list:
print(link)
# Print the total number of items found
print(f"Total number of items found: {len(link_list)}")
# Close the WebDriver
driver.quit()