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"]
原文地址:https://www.cnblogs.com/pualus/p/8616228.html