首页 > 解决方案 > 'NoneType' 对象在使用 beautifulsoup 进行网络爬行时没有属性'text'

问题描述

我在尝试抓取网络数据时出错,错误消息说

标签信息:“NoneType”对象没有属性“文本”

该网站在这里 https://www.target.com/p/nong-shim-noodle-bowl-soup-spicy-kimchi-flavor-3-03oz/-/A-15137591#lnk=sametab

        try:
            label_info = soup.find('div', {'class': 'h-bg-white h-margin-a-default'})
            if debug: print('Label info:',label_info.text)
            if debug: print('')
        except Exception as e:
            label_info = ''
            if debug: print("Label info:", e)
            pass

谁能指导我为什么这不起作用?

标签: pythonbeautifulsoupweb-crawler

解决方案


您可以将 CSS 类指定为字符串列表,而不是一个字符串:

import requests
from bs4 import BeautifulSoup

url = 'https://www.target.com/p/nong-shim-noodle-bowl-soup-spicy-kimchi-flavor-3-03oz/-/A-15137591#lnk=sametab'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

print(soup.find('div', {'class': ['h-bg-white', 'h-margin-a-default']}).get_text(strip=True, separator='\n'))

印刷:

Highlights
Spicy kimchi flavor
Fresh noodle texture and robust flavor
Ready in only 3 minutes— just add hot water
Convenient on-the-go meal

推荐阅读