首页 > 解决方案 > TypeError:“NoneType”对象不可下标如何使用属性

问题描述

初学者网络抓取错误

TypeError:“NoneType”对象不可下标

res=requests.get("https://www.brainyquote.com/quote_of_the_day")
soup=BeautifulSoup(res.text,'lxml')

image_quote=soup.find('img', {'class':'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive'})

print(image_quote['alt'])

我希望输出是当天的报价。而是收到此错误。

标签: python-3.xweb-scrapingbeautifulsoup

解决方案


试试下面的代码: -

import requests
from bs4 import BeautifulSoup
# getting HTML from the Google Play web page
res=requests.get("https://www.brainyquote.com/quote_of_the_day")
soup=BeautifulSoup(res.text,'lxml')
image_quote = soup.find('img', {'class':'p-qotd'})
print(image_quote['alt'])
# print(soup)

您的代码不起作用,因为您已指定所有类。只需指定唯一的类


推荐阅读