python爬虫错误

错误描述


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"]
posted @ 2018-03-21 12:08  屋中人  阅读(366)  评论(0编辑  收藏  举报
返回顶部