使用Python的BeautifulSoup上的属性错误(web抓取)

使用Python的BeautifulSoup上的属性错误(web抓取),python,web-scraping,beautifulsoup,amazon-product-api,Python,Web Scraping,Beautifulsoup,Amazon Product Api,我正在学习使用Python进行web抓取的教程,到目前为止,我有以下内容: import requests from bs4 import BeautifulSoup URL = 'https://www.amazon.de/JBL-Charge-Bluetooth-Lautsprecher-Schwarz- integrierter/dp/B07HGHRYCY/ref=sr_1_2_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&am

我正在学习使用Python进行web抓取的教程,到目前为止,我有以下内容:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.amazon.de/JBL-Charge-Bluetooth-Lautsprecher-Schwarz-      integrierter/dp/B07HGHRYCY/ref=sr_1_2_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&  keywords=jbl+charge+4&qid=1562775856&s=gateway&sr=8-2-spons&psc=1'
headers = {
    "User-Agent": 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Mobile Safari/537.36'}
page = requests.get(URL,headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.find(id="productTitle").get_text()
print(title.strip())





我试图从Amazon打印某个产品的名称,但我得到了以下错误:AttributeError:“NoneType”对象没有属性“get_text”,每当我尝试从BeautifulSoup库运行get_text方法时。如何成功打印产品名称?

由于选择器没有找到合适的元素,因此get\u文本不起作用,而是返回了None。因此,您在一个没有get_text方法的空元素上调用它。我不确定为什么id=productTitle在查看它应该显示的HTML时不起作用。但是,您可以使用不同的选择器并获取其上方的div,以获得类似的结果:

title = soup.find(id="title").get_text()
print(title.strip())
其输出为:

"JBL Charge 4 Bluetooth-Lautsprecher in Schwarz, Wasserfeste, portable Boombox mit integrierter Powerbank, Mit nur einer Akku-Ladung bis zu 20 Stunden kabellos Musik streamen"
get_文本不起作用,因为选择器没有找到合适的元素,而是返回了None。因此,您在一个没有get_text方法的空元素上调用它。我不确定为什么id=productTitle在查看它应该显示的HTML时不起作用。但是,您可以使用不同的选择器并获取其上方的div,以获得类似的结果:

title = soup.find(id="title").get_text()
print(title.strip())
其输出为:

"JBL Charge 4 Bluetooth-Lautsprecher in Schwarz, Wasserfeste, portable Boombox mit integrierter Powerbank, Mit nur einer Akku-Ladung bis zu 20 Stunden kabellos Musik streamen"
请尝试以下操作:

title = soup.find('span', id="productTitle").get_text()
这应该行得通。

请尝试以下操作:

title = soup.find('span', id="productTitle").get_text()
这应该行得通