首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium不能使用python刮Shopee电子商务站点。

Selenium不能使用python刮Shopee电子商务站点。
EN

Stack Overflow用户
提问于 2019-04-21 15:08:44
回答 3查看 4.3K关注 0票数 3

我无法在Shopee (一个电子商务网站)上提取产品的价格.

我看了@dmitrybelyakov (链接:使用python抓取AJAX电子商务站点)解决的问题。

这个解决方案帮助我获得了产品的‘名称’和'historical_sold‘,但我无法得到产品的价格。我找不到Json字符串中的价格值。因此,我尝试使用selenium来使用xpath来提取数据,但它似乎失败了。

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

我的代码:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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上得到产品的价格!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-21 21:05:42

要使用肖佩提取产品的价格,可以使用以下解决方案:

  • 代码块: 从selenium.webdriver.support.ui导入from驱动程序从selenium.webdriver.common.by导入WebDriverWait从selenium.webdriver.common.by导入expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument(‘-无头’)options.add_argument(‘开始-最大化’)options.add_argument(‘禁用-infobars’)options.add_argument(‘-禁用-扩展’)浏览驱动程序= webdriver.Chrome(chrome_options=options,WebDriverWait browserdriver.get('https://shopee.com.my/search?keyword=h370m') WebDriverWait(浏览器驱动程序),20).until(EC.element_to_be_clickable((By.XPATH,"//div@class='shopee-modal__container'//buttontext()='English'"))).click() print([my_element.text for my_element in WebDriverWait(浏览器驱动程序,20).until(EC.visibility_of_all_elements_located((By.XPATH,“//spantext()=‘RM’//以下:span1 1”))
  • 控制台输出: “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”节目结束
票数 1
EN

Stack Overflow用户

发布于 2019-04-21 16:41:20

您可以对站点使用请求和搜索API。

代码语言:javascript
复制
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'])

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

代码语言:javascript
复制
for item in r['items']:
    print(item['name'], ' ', 'RM' + str(item['price']/100000))
票数 3
EN

Stack Overflow用户

发布于 2019-04-21 15:25:45

在访问网站的时候。我遇到了一个弹出的https://gyazo.com/0a9cd82e2c9879a1c834a82cb15020bd。我猜想,selenium为什么不能检测您正在寻找的xpath,是因为这个弹出阻止了元素。

在启动selenium会话之后,尝试如下:

代码语言:javascript
复制
popup=browserdriver.find_element_by_xpath('//*[@id="modal"]/div[1]/div[1]/div/div[3]/button[1]')
popup.click()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55783931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档