首页 > 解决方案 > 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?

标签: pythonweb-scrapingbeautifulsoupamazon-product-api

解决方案


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"

推荐阅读