我刚刚启动了一个跟踪亚马逊价格的应用程序项目。根据教程视频,在此步骤中,当我运行代码时,它必须显示产品的名称和价格,但是唯一会出现的错误是下一个错误: AttributeError:'NoneType'对象没有属性'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())
答案 0 :(得分:0)
您的脚本几乎是正确的,只需使用lxml
或html5lib
解析器而不是html.parser
:
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, 'lxml') # <-- 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(',', '.')) # <-- change the conversion method
print(converted_price)
print(title.strip())
打印:
69.99
Tablet Fire 7, pantalla de 7'', 16 GB (Negro) - Incluye ofertas especiales