【发布时间】:2013-01-21 18:24:45
【问题描述】:
我正在尝试解析表单的 solr 输出:
<doc>
<str name="source">source:A</str>
<str name="url">URL:A</str>
<date name="p_date">2012-09-08T10:02:01Z</date>
</doc>
<doc>
<str name="source">source:B</str>
<str name="url">URL:B</str>
<date name="p_date">2012-08-08T11:02:01Z</date>
</doc>
我热衷于使用漂亮的汤(具有 BeautifulStoneSoup 的版本;我认为在 BS4 之前)来解析文档。 我使用了漂亮的汤来进行 HTML 解析,但有些我无法找到一种有效的方法来提取标签的内容。
我写过:
for tags in soup('doc'):
print tags.renderContents()
我确实感觉到我可以强制通过它来获得输出(比如再次说“汤”),但我希望有一个有效的解决方案来提取数据。 我需要的输出是:
source:A
URL:A
2012-09-08T10:02:01Z
source:B
URL:B
2012-08-08T11:02:01Z
谢谢
【问题讨论】:
-
renderContents() 只是将每个子树转换为 HTML。为什么不遍历所有 src 节点并输出它们的 text 属性?
标签: python parsing solr xml-parsing beautifulsoup