webpages get duplicated with both Selenium and BeautifulSoup

Viewed 39

I try to scrape the webpage https://www.lazada.sg/the-sg-mart/?from=wangpu&langFlag=en&page=1&pageTypeId=2&q=All-Products&sort=pricedesc , but working with both bs4 and Selenium, the pages get duplicated after some point (generally after page 35 or so). This is not the case when I normally browse the website. Here is my code

driver = webdriver.Chrome(service=chrome_driver_path)
driver.get('https://www.lazada.sg/the-sg-mart/?from=wangpu&langFlag=en&page=1&pageTypeId=2&q=All-Products&sort=pricedesc')


names = []
prices = []

soup_names = []
soup_prices = []
while True:
    for i in range(1, 41):
        path = '//*[@id="root"]/div/div[3]/div[1]/div/div[1]/div[2]/div[' + str(i) + ']/div/div/div[2]/div[2]/a'    
        name = driver.find_element(By.XPATH, path).text
        if name in names:
            print(i)
            pass

        path_price        =    '//*[@id="root"]/div/div[3]/div[1]/div/div[1]/div[2]/div[' + str(i) + ']/div/div/div[2]/div[3]/span'   
        price = driver.find_element(By.XPATH, path_price) .text
        
        names.append(name)
        prices.append(price)
     
        
        for item in soup.find_all(class_='RfADt'):
            print(item.a.get('title'))
            soup_names.append(item.a.get('title'))
        
        for item in soup.find_all(class_='aBrP0'):
            print(item.text)
            soup_prices.append(item.text)
    
   
    
    button=driver.find_element_by_xpath("//li[@title='Next Page']")
    driver.execute_script("arguments[0].click();", button)
    print('new page')
    sleep(5)

How can I prevent that and why does it even happen in the first place?

0 Answers
Related