我想改变这个
def has_class_but_no_id(tag):
return tag.has_key('class') and not tag.has_key('id')
此函数来自 Python2,不适用于 Python3
我以为
我在这样的列表中更改了这个 HTML 文档
list_of_descendants = list(soup.descendants)
所以我可以获得包含类但不包含 id 的标签 它是关于找到所有带有 class = blabla... 但不是 id = ... 的标签 我不知道如何处理这个问题
请您参考如下方法:
I renamed one method for compatibility with Python 3:
Tag.has_key()->Tag.has_attr()
此外,documentation here 中提供了完全相同的功能。 :
If none of the other matches work for you, define a function that takes an element as its only argument. The function should return
Trueif the argument matches, andFalseotherwise.Here’s a function that returns
Trueif a tag defines the “class” attribute but doesn’t define the “id” attribute:def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id')
