我试图从沃尔玛的一个页面中榨取价格,但收到错误。以下是我的代码:
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的类似错误。
有人可以帮忙吗?
我查看了给定的URL,价格组似乎是类名而不是ID。所以你需要:
price = soup.find(class_="price-group").get_text()