一些 html 代码包含一些 dt
标签,如下所示:
<dt>PLZ:</dt>
<dd>
8047
</dd>
我想在 dt
标记后面的 dd
标记中查找带有文本 PLZ:
的文本。根据文档,我正在尝试以下操作:
number = BeautifulSoup(text).find("dt",text="PLZ:").findNextSiblings("dd")
with text
上面的字符串,但我得到的只是一个空列表,而不是我正在寻找的数字(当然是字符串)。也许我误解了文档?
最佳答案
所以尝试一下:
from BeautifulSoup import BeautifulSoup
text = """
<dt>PLZ:</dt>
<dd>
8047
</dd>"""
number = BeautifulSoup(text).find("dt",text="PLZ:").parent.findNextSiblings("dd")
print BeautifulSoup(''.join(number[0]))
或者如果您使用 findNext 找到尝试:
number = BeautifulSoup(text).find("dt",text="PLZ:").parent.findNext("dd").contents[0]
关于python - 使用 beautifulsoup 解析带有一些文本的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103195/