Python 求助一个 beautifulsoap 的问题

married577-github · 2019年09月30日 · 最后由 hellohell 回复于 2019年10月02日 · 1354 次阅读

代码和报错如下:

共收到 3 条回复 时间 点赞

是想 解析 标签下的 吗?

可以试试:
data = soup.find_all('span',{'class': 'songlist_songname_txt'})

for na in data:
print(na.fiind_all('a'))

find_all 返回的是数组,如果想返回一个要么用取索引,要么用 find

data  是 bs4.element.ResultSet 类的对象,bs4.element.ResultSetlist 的子类,它本身没有拓展 list ,不支持 find_all 方法。

class ResultSet(list):
    """A ResultSet is just a list that keeps track of the SoupStrainer
    that created it."""
    def __init__(self, source, result=()):
        super(ResultSet, self).__init__(result)
        self.source = source

    def __getattr__(self, key):
        raise AttributeError(
            "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
        )

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册