TypeError:“ NoneType”对象不可下标如何使用属性

时间:2019-08-13 05:33:44

标签: python-3.x web-scraping beautifulsoup

初学者通过网络抓取错误

  

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'])

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

1 个答案:

答案 0 :(得分:0)

尝试以下代码:-

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)

您的代码无效,因为您指定了所有类。只需指定唯一的类