Huynh
Huynh

Reputation: 392

Selenium can not scrape Shopee e-commerce site using python

I am not able to pull the price of products on Shopee (a e-commercial site).
I have taken a look at the problem solved by @dmitrybelyakov (link: Scraping AJAX e-commerce site using python) .

That solution helped me to get the 'name' of product and the 'historical_sold' but I can not get the price of the product. I can not find the price value in the Json string. Therefore, I tried to use selenium to pull data with xpath but it appeared to be failed.

The link of the ecommercial site: https://shopee.com.my/search?keyword=h370m

My code:

import time

from selenium import webdriver

import pandas as pd

path = r'C:\Users\\admin\\Desktop\\chromedriver_win32\\Chromedriver'

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1200x600')

browserdriver = webdriver.Chrome(executable_path = path,options=chrome_options)
link='https://shopee.com.my/search?keyword=h370m'
browserdriver.get(link)
productprice='//*[@id="main"]/div/div[2]/div[2]/div/div/div/div[2]/div/div/div[2]/div[1]/div/a/div/div[2]/div[1]'
productprice_printout=browserdriver.find_element_by_xpath(productname).text
print(productprice_printout)

When I run that code, it showed the error notification like this:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]/div/div[2]/div[2]/div/div/div/div[2]/div/div/div[2]/div[1]/div/a/div/div[2]/div[1]"}

Please help me to get the price of product on Shopee!

Upvotes: 3

Views: 5561

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193198

To extract the price of products on Shopee using Selenium and Python you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    browserdriver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    browserdriver.get('https://shopee.com.my/search?keyword=h370m')
    WebDriverWait(browserdriver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='shopee-modal__container']//button[text()='English']"))).click()
    print([my_element.text for my_element in WebDriverWait(browserdriver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text()='RM']//following::span[1]")))])
    print("Program Ended")
    
  • Console Output:

    ['430.00', '385.00', '435.00', '409.00', '479.00', '439.00', '479.00', '439.00', '439.00', '403.20', '369.00', '420.00', '479.00', '465.00', '465.00']
    Program Ended
    

Upvotes: 1

QHarr
QHarr

Reputation: 84465

You can use requests and the search API for the site

import requests

headers = {
    'User-Agent': 'Mozilla/5',
    'Referer': 'https://shopee.com.my/search?keyword=h370m'
}

url = 'https://shopee.com.my/api/v2/search_items/?by=relevancy&keyword=h370m&limit=50&newest=0&order=desc&page_type=search'  
r = requests.get(url, headers = headers).json()

for item in r['items']:
    print(item['name'], ' ', item['price'])

If you want roughly the same scale:

for item in r['items']:
    print(item['name'], ' ', 'RM' + str(item['price']/100000))

Upvotes: 3

Jake Strouse
Jake Strouse

Reputation: 58

When visiting the website. I come across this popup https://gyazo.com/0a9cd82e2c9879a1c834a82cb15020bd. I am guessing, why selenium cannot detect the xpath you are looking for, is because this popup is blocking the element.

right after starting the selenium session, try this:

popup=browserdriver.find_element_by_xpath('//*[@id="modal"]/div[1]/div[1]/div/div[3]/button[1]')
popup.click()

Upvotes: 0

Related Questions