首页 > 编程语言 > 详细

python爬虫错误

时间:2018-03-21 13:35:19      阅读:185      评论:0      收藏:0      [点我收藏+]

错误描述


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

错误缘由


取标签属性的时候,
find_all()函数与find()函数的不同造成的错误

详解


find_all()

无论找到几个,该函数均返回 list

find()

返回符合条件的第一个标签,返回的是 标签

而取属性的时候,必须要用到 标签

coding:

href = tr.find("a")["href"]

# 等同于
href = tr.find_all("a")[0]["href"]

python爬虫错误

原文:https://www.cnblogs.com/pualus/p/8616228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!