"NoneType"对象在带有美丽汤的网络爬行中没有属性"文本"



我有一个错误,而试图抓取web数据,错误信息说

标签信息:'NoneType'对象没有属性'text'

网站在这里https://www.target.com/p/nong垫片-面条碗汤-辣泡菜风味- 3 - 03 - oz//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

谁能告诉我为什么这不起作用?

可以将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

最新更新