您好,我正在尝试使用python上漂亮的汤4包确定亚马逊商品的价格,但我不断收到none类型对象错误,因此找不到价格
我尝试切换span和id元素以查看是否可能引起问题,但它没有任何改变
my python code:
amazon_price_checker('https://www.amazon.com/Merax-Finiss-Aluminum-Mountain-
Brakes/dp/B016A59WPM')
def amazon_price_checker(amazon_link):
print(amazon_link)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 \
Safari/537.36',
}
res = requests.get(amazon_link, headers=headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, 'html.parser')
price = soup.find("span", {"id": "priceblock_ourprice"}).get_text()
print(price)
html code on website:
<span id="priceblock_ourprice" class="a-size-medium a-color-price
priceBlockBuyingPriceString">$239.99</span>
我希望输出为239.99,但会收到此错误
https://www.amazon.com/Merax-Finiss-Aluminum-Mountain-Brakes/dp/B016A59WPM
Traceback (most recent call last):
price = soup.find("span", {"id": "priceblock_ourprice"}).get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'
答案 0 :(得分:0)
看起来像是html.parser
的错误(或者原始html无效)。改为使用lxml
或html5lib
soup = BeautifulSoup(res.text, 'lxml')
打印出
$239.99