Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

这篇文章主要介绍了Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在python的Beautiful Soup 4 扩展库的使用过程中出现了

TypeError: list indices must be integers or slices, not str

这个错误,这里就分析一下为什么会报错以及如何解决。

这个错误的意思是’类型错误:list的索引必须是’integers’或者’slices’不能是’str’

我出现错误的代码:

#引入库
from bs4 import BeautifulSoup
#读取页面
soup = BeautifulSoup(open('index.html'))
#获取标签
img_tag = div.select("img")
#获取标签属性(这里报错)
src = img_tag['src']
#输出
print(src)

经过检查对比后我发现错误原因

就是获取标签时获取的是list数据而不是tag

主要原因如下:
主要就是获取的内容和自己认为的有偏差。

也就是find()和find_all(),select()和select_one()的区别。
当使用

find()
select_one()

时,获得的是一个标签
类型为

<class 'bs4.element.Tag'>

所以可以使用tag[

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当出现"TypeError: list indices must be integers or slices, not str"错误时,意味着你尝试使用字符串而不是整数或切片作为列表的索引。这个错误通常发生在你试图通过字符串来访问列表中的元素时。 要解决这个错误,你需要确保你的索引是一个整数或切片。你可以检查你使用的索引是否正确,并确保它是整数类型。另外,你还可以确认你正在处理的对象是一个列表而不是其他类型的对象。如果你使用的是切片,你可以验证切片的起始和结束位置是否都是整数类型。 如果你遇到这个错误的问题是因为导入了错误的模块或使用了错误的函数,你需要检查你的代码并确保你导入和使用的是正确的模块和函数。 总之,当你遇到"TypeError: list indices must be integers or slices, not str"错误时,你需要检查你的索引是否是整数或切片,确保你正在处理的对象是一个列表,并验证你导入和使用的模块和函数是否正确。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [已解决TypeErrorList indices must be integers or slices , not str](https://blog.csdn.net/yuan2019035055/article/details/126149910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str](https://download.csdn.net/download/weixin_38590567/14871394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [python 报错 TypeError: tuple indices must be integers or slices, not tuple](https://blog.csdn.net/A__MP/article/details/125231761)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值