python - Attribute error on BeautifulSoup with Python (web scraping)
问题描述
I am following a tutorial on web scraping with Python and so far I have this:
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())
I am trying to print the name of some product from Amazon, but I get this error: AttributeError: 'NoneType' object has no attribute 'get_text', whenever I try to run the get_text() method from the BeautifulSoup library. How can I successfully print the name of the product?
解决方案
The get_text()
doesnt work because your selector didnt find a suitable element and returned None
instead. So you are calling it on an empty element which doesnt have the get_text()
method. I am unsure as to why id=productTitle
doesnt work as looking at the HTML it should imo. However you could use a different selector and get the div above it instead to get similar result:
title = soup.find(id="title").get_text()
print(title.strip())
Output of that is:
"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"
推荐阅读
- angular - 单向数据绑定不适用于 FullCalendar 和 Angular
- python - Scrapy分页无限
- google-cloud-platform - 启动虚拟机时出错 - 可用资源不足
- angular - 角度 6 错误对象为空以处理异常
- php - 如何循环遍历 mysql 查询的结果并将它们显示在 html 表单的选项值中
- javascript - 导入和使用 NPM 包
- playframework - 创建actor时的新play 2.6 application.conf默认值和最佳实践
- c# - 以相反的顺序将数组值传递给另一个数组
- .net-core - .Net Core Debug Logger 在日志级别打印工件
- c - 使用 libtiff 在 C 中读取 .tif 图像返回一列