Bs4 AttributeError:“NoneType”对象没有属性“get_text”

我刚刚开始了一个跟踪亚马逊价格的应用程序项目。根据教程视频,在这一步,当我运行代码时,它必须显示产品的名称和价格,但唯一出现的是下一个错误:AttributeError: 'NoneType' object has no attribute 'get_text'。我已经在其他页面上尝试过,并在这里修改了相同的主题,但没有找到答案。请帮忙 ;(


代码本身:


import requests

from bs4 import BeautifulSoup


URL = 'https://www.amazon.es/dp/B07JQRWLXM/ref=nav_signin?pf_rd_r=FJX3CJC8RWFE3NPQJRNP&'


headers = {

"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 

 Chrome/86.0.4240.75 Safari/537.36'}


 page = requests.get(URL, headers=headers)


 soup = BeautifulSoup(page.content, 'html.parser')


 title = soup.find(id="productTitle").get_text()

 price = soup.find(id = "priceblock_ourprice").get_text()

 converted_price = float(price[0:3])


 print(converted_price)

 print(title.strip())


莫回无
浏览 81回答 1
1回答

UYOU

您的脚本几乎是正确的,只需使用lxml或html5lib解析器而不是html.parser:import requestsfrom bs4 import BeautifulSoupURL = 'https://www.amazon.es/dp/B07JQRWLXM/ref=nav_signin?pf_rd_r=FJX3CJC8RWFE3NPQJRNP&'headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}page = requests.get(URL, headers=headers)soup = BeautifulSoup(page.content, 'lxml')&nbsp; &nbsp;# <-- change to 'lxml' or 'html5lib'title = soup.find(id="productTitle").get_text()price = soup.find(id = "priceblock_ourprice").get_text()converted_price = float(price.split()[0].replace(',', '.'))&nbsp; &nbsp;# <-- change the conversion methodprint(converted_price)print(title.strip())印刷:69.99Tablet Fire 7, pantalla de 7'', 16 GB (Negro) - Incluye ofertas especiales
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python