Selenium无法使用python抓取Shopee电子商务网站

时间:2019-04-21 15:08:44

标签: python selenium xpath web-scraping request

我无法在Shopee(电子商务网站)上拉高产品价格。
 我看了@dmitrybelyakov(链接:Scraping AJAX e-commerce site using python)解决的问题。

该解决方案帮助我获得了产品的“名称”和“ historical_old”,但我无法获得产品的价格。我在Json字符串中找不到价格值。 因此,我尝试使用硒通过xpath提取数据,但似乎失败了。

电子商务网站的链接:https://shopee.com.my/search?keyword=h370m

我的代码:

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)

当我运行该代码时,它显示如下错误通知:

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]"}

请帮助我在Shopee上获得产品价格!

3 个答案:

答案 0 :(得分:3)

您可以使用网站的请求和搜索API

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'])

如果您想要大致相同的比例:

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

答案 1 :(得分:1)

要使用Shopee Python 提取Selenium上的产品价格,可以使用以下解决方案:

  • 代码块:

    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")
    
  • 控制台输出:

    ['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
    

答案 2 :(得分:0)

访问网站时。我遇到了这个弹出窗口https://gyazo.com/0a9cd82e2c9879a1c834a82cb15020bd。我猜,硒为什么无法检测到您正在寻找的xpath,是因为此弹出窗口阻止了该元素。

在开始硒会话后,立即尝试以下操作:

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