【发布时间】:2019-10-09 23:59:57
【问题描述】:
我正在尝试使用 xml.etree 模块复制 BeautifulSoup 的 find_all 功能。 由于某些原因,我们不允许使用 bs4 包,因此 Beautiful soup 不在等式。 有什么方法可以搜索特定标签,然后存储标签的每一行直到结束?
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<State name="Singapore"><State name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</State>
我需要类似的东西,在列表中获取 State 标记的详细信息。
[<State name="Singapore">,<rank>4</rank>,.....,'</state>']
不幸的是,当我尝试遍历 XML 文件时,它给了我一个包含确切内容的对象。 .attrib 为我返回一个字典。
【问题讨论】:
-
您的示例不是有效的 XML。
-
是什么阻止了您根据“iter”为您提供的内容构建列表?
标签: python python-3.x xml.etree