我在 BeautifulSoup 中发现了一些奇怪的行为,如下面的示例所示。
import re
from bs4 import BeautifulSoup
html = """<p style='color: red;'>This has a <b>color</b> of red. Because it likes the color red</p>
<p class='blue'>This paragraph has a color of blue.</p>
<p>This paragraph does not have a color.</p>"""
soup = BeautifulSoup(html, 'html.parser')
pattern = re.compile('color', flags=re.UNICODE+re.IGNORECASE)
paras = soup.find_all('p', string=pattern)
print(len(paras)) # expected to find 3 paragraphs with word "color" in it
2
print(paras[0].prettify())
<p class="blue">
This paragraph as a color of blue.
</p>
print(paras[1].prettify())
<p>
This paragraph does not have a color.
</p>
正如您所看到的,由于某种原因<p style='color: red;'>This has a <b>color</b> of red. Because it likes the color red</p>
的第一段没有被 find_all(...)
拾取我不明白为什么不。
最佳答案
string
属性期望标记仅包含文本而不包含标记。如果您尝试为第一个 p
标记打印 .string
,它将返回 None
,因为它包含标记。
或者,为了更好地解释它,documentation说:
If a tag has only one child, and that child is a
NavigableString
, the child is made available as.string
If a tag contains more than one thing, then it’s not clear what
.string
should refer to, so.string
is defined to beNone
克服这个问题的方法是使用 lambda
函数。
html = """<p style='color: red;'>This has a <b>color</b> of red. Because it likes the color red</p>
<p class='blue'>This paragraph has a color of blue.</p>
<p>This paragraph does not have a color.</p>"""
soup = BeautifulSoup(html, 'html.parser')
first_p = soup.find('p')
print(first_p)
# <p style="color: red;">This has a <b>color</b> of red. Because it likes the color red</p>
print(first_p.string)
# None
print(first_p.text)
# This has a color of red. Because it likes the color red
paras = soup.find_all(lambda tag: tag.name == 'p' and 'color' in tag.text.lower())
print(paras)
# [<p style="color: red;">This has a <b>color</b> of red. Because it likes the color red</p>, <p class="blue">This paragraph has a color of blue.</p>, <p>This paragraph does not have a color.</p>]
关于python - BeautifulSoup find_all() 未找到所有请求的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49338402/