Walmart.com使用Python和bs4进行价格刮擦

问题描述 投票:0回答:1

我试图从沃尔玛的一个页面中榨取价格,但收到错误。以下是我的代码:

import requests
from bs4 import BeautifulSoup

URL = "https://www.walmart.com/ip/Wilson-The-Duke-Official-NFL-Game-Football/5192758"
page = requests.get(URL,headers={"User-Agent":"Defined"})
soup = BeautifulSoup(page.content, "html.parser")
price = soup.find(id="price-group").get_text()
print(price)

我在命令行上得到以下输出:

回溯(最近一次调用最后一次):文件“walmart.py”,第7行,在price = soup.find(id =“price-group”)。get_text()AttributeError:'NoneType'对象没有属性'get_text'

我看到了Nordstrom和Sears的类似错误。

有人可以帮忙吗?

python python-3.x python-2.7 python-requests
1个回答
3
投票

我查看了给定的URL,价格组似乎是类名而不是ID。所以你需要:

price = soup.find(class_="price-group").get_text()
© www.soinside.com 2019 - 2024. All rights reserved.