python爬虫错误

时间:2018-03-21 14:07:21   收藏:0   阅读:1384

错误描述


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"]
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!