python - 使用 beautifulsoup 解析带有一些文本的标签

标签 python web-scraping beautifulsoup

一些 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/

相关文章:

Python,在已知字符串正下方的行中搜索文本?

python beautifulsoup 将属性添加到没有值的标签

python - 使用 beautifulsoup 进行网页抓取 - 导航

python - 使用python解析相对链接和绝对链接

python - PIP fatal error

python - 使用 Python 将值列表传递给 Oracle

java - 尝试使用 Jsoup 从 Web 抓取动态数据到 Android 应用程序,但没有获取准确的数据

python - 无法使用搜索关键字解析网页中的某些信息

python - 使用文件监视通过 PyQt4 刷新日志查看器

Python简单输入错误