布尔不是字符串等价
问题描述:
我解析一些XML与BeautifulSoup,并有看起来像数据登记为真。所以我有:布尔不是字符串等价
for e in soup.findAll('title'):
if e == '<title>main title</title>':
pass
else:
print (e)
这仍然返回所有标题,包括main title
。我试过删除标题标签。
感谢您的任何帮助。
答
您布尔不能正常工作,它将永远是假的。你可以将它转换为字符串然后比较它们。
试试这个:
for e in soup.find_all("title"):
if str(e) == '<title>main title</title>':
pass
else:
print (e)
输出:
<title>other title</title>
<title>another title</title>
答
您可以检查节点的文本属性,而不是节点本身。
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<title>main title</title>
<title>other title</title>
<title>another title</title>""", "html.parser")
for e in soup.find_all("title"):
if e.text != 'main title':
print(e)
#<title>other title</title>
#<title>another title</title>
答
如果你想跳过第一个冠军,然后更好的解决方案是切片名单:
>>> soup.findAll('title')[1:]
[<title>other title</title>, <title>another title</title>]
只要你想一个对象
<class 'bs4.element.Tag'>
比较字符串