Python 我想用bs4从亚马逊获得一个产品的名称

Python 我想用bs4从亚马逊获得一个产品的名称,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我想通过BeuatifulSoup和requests获得此亚马逊产品的标题。当我运行时,这是说: Traceback (most recent call last): File "scraper.py", line 15, in <module> title = soup.find('span', id='productTitle').get_text() AttributeError: 'NoneType' object has no attribu

我想通过BeuatifulSoup和requests获得此亚马逊产品的标题。当我运行时,这是说:

Traceback (most recent call last):
  File "scraper.py", line 15, in <module>
    title = soup.find('span', id='productTitle').get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'

问题是如何使用“html.parser”作为bs4解析器。改用lxml(这将更优雅地处理损坏的html)。该错误试图告诉您它从未找到
——我们可以看到它就在那里,因此可能是与非标准HTML相关的解析失败

导入请求
从bs4导入BeautifulSoup
url='1〕https://www.amazon.de/OnePlus-Smartphone-Almond-Display-Speicher/dp/B07RWL3K1Y/ref=sr_1_2? __mk_de_de=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=oneplus+7+pro&qid=1598088298&sr=8-2'
标题={
“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/84.0.4147.135 Safari/537.36”
}
page=requests.get(url,headers=headers)
汤=美汤(page.content,'lxml')
title=soup.find('span',id='productTitle')。get_text().strip()
印刷品(标题)
输出:

OnePlus 7 Pro Smartphone Almond (16,9 cm) AMOLED Display 8 GB RAM + 256 GB Speicher, Triple Kamera (48 MP) Pop-up Kamera (16 MP) – Dual SIM Handy
OnePlus 7 Pro Smartphone Almond (16,9 cm) AMOLED Display 8 GB RAM + 256 GB Speicher, Triple Kamera (48 MP) Pop-up Kamera (16 MP) – Dual SIM Handy