gpt4 book ai didi

Python BeautifulSoup - find 和 findAll 的不同结果

转载 作者:行者123 更新时间:2023-11-28 17:51:02 24 4
gpt4 key购买 nike

我正在尝试使用 BeautifulSoup 解析嵌入在 HTML 中的一些文本,使用“文本”属性。玩“find”和“findAll”时,我注意到一些奇怪的事情,我无法理解为什么它会以这种方式表现......

import re
import BeautifulSoup

doc = "<html><head><title>Page title</title><title>Author name</title></head>"
soup = BeautifulSoup(doc)

# find test
test1 = soup1.find('title',text=re.compile("Page"))
print test1 # Returns "Page title"

# findAll test
test2 = soup1.findAll('title',text=re.compile("Page"))
print test2 # Returns "[<title>Page title</title>, <title>Author name</title>]"

在第二个测试中,解析器不应该返回与第一个示例相同的结果吗?第二个测试应该返回所有文本包含“Page”的“title”标签,但它也返回第二个 title 标签。

这是预期的还是我遗漏了什么?

最佳答案

The documentation似乎暗示 textname 参数是互斥的。但是,它说,如果指定了 text,则 name 将被忽略,在您的情况下,它似乎做相反的事情。

关于Python BeautifulSoup - find 和 findAll 的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9819141/

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