gpt4 book ai didi

python - BeautifulSoup 中带有 .get-operator 的 if 语句

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:52 25 4
gpt4 key购买 nike

我正在尝试获取 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 因为 classmulti-valued attribute .

检查方法是否在列表内:

if 'method' in child.dl.get('class', []):
print("WORKS!")

关于python - BeautifulSoup 中带有 .get-operator 的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com