我正在关注有关使用Python进行网页抓取的教程,到目前为止,我已经掌握了以下内容:
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())
我试图从亚马逊打印某些产品的名称,但是出现此错误:AttributeError:每当我尝试从BeautifulSoup库运行get_text()方法时,“ NoneType”对象都没有属性“ get_text”。如何成功打印产品名称?