我无法在 Shopee(电子商务网站)上提取产品价格。
我查看了@dmitrybelyakov(链接:Scraping AJAX e-commerce site using python)解决的问题。
该解决方案帮助我获得了产品的“名称”和“历史销售”,但我无法获得产品的价格。我在 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上的产品价格!
最佳答案
您可以使用网站的请求和搜索 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))
关于python - Selenium 无法使用 python 抓取 Shopee 电子商务网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55783931/