python-3.x - 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'])
我希望输出是当天的报价。而是收到此错误。
解决方案
试试下面的代码: -
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)
您的代码不起作用,因为您已指定所有类。只需指定唯一的类
推荐阅读
- python - python - 为什么在python中使用pickle编码和解码后列表会发生变化?
- python - 仅从第一页提取的数据保存在 csv 文件中(Scrapy、Selenium)
- java - DecimalFormatSymbols 具有非正统语言环境的意外区域特定值
- html - Puppeteer 没有填写输入元素/错误的选择器?
- git - 有没有办法在主分支上切换已提交更改的分支?
- python - 从qabstractitemmodel继承的pyside2 qsqltablemodel类中定义的setHeaderData在哪里?
- python - 当行数很高时如何合并两个数据框?
- c# - c#脚本中的可为空的日期时间
- kubernetes - Kubernetes 备用/冷副本/Pod
- avro - Avro 架构无法通过连接器反序列化自动注册的 avro 架构