我正在尝试获取 dl 元素的类。我可以打印该类(请参阅第一行),但我无法在 if 语句中使用该结果(因此永远不会打印“works”)。 我想我的语法有些错误。我是吗? 相信我,我的测试中有很多“方法”类元素 - 见下文
print(child.dl.get('class'))
if child.dl.get('class')=="['method']":
print("WORKS!")
这是第一行的输出:
['method']
['method']
['class']
['method']
['method']
['method']
['method']
['describe']
最佳答案
child.dl.get('class')
返回一个 list
因为 class
是 multi-valued attribute
.
检查方法
是否在列表内:
if 'method' in child.dl.get('class', []):
print("WORKS!")
关于python - BeautifulSoup 中带有 .get-operator 的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224802/