我无法在Shopee (一个电子商务网站)上提取产品的价格.
我看了@dmitrybelyakov (链接:使用python抓取AJAX电子商务站点)解决的问题。
这个解决方案帮助我获得了产品的‘名称’和'historical_sold‘,但我无法得到产品的价格。我找不到Json字符串中的价格值。因此,我尝试使用selenium来使用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上得到产品的价格!
发布于 2019-04-21 21:05:42
发布于 2019-04-21 16:41:20
您可以对站点使用请求和搜索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))发布于 2019-04-21 15:25:45
在访问网站的时候。我遇到了一个弹出的https://gyazo.com/0a9cd82e2c9879a1c834a82cb15020bd。我猜想,selenium为什么不能检测您正在寻找的xpath,是因为这个弹出阻止了元素。
在启动selenium会话之后,尝试如下:
popup=browserdriver.find_element_by_xpath('//*[@id="modal"]/div[1]/div[1]/div/div[3]/button[1]')
popup.click()https://stackoverflow.com/questions/55783931
复制相似问题