【问题标题】:getting tag names at one level with Beautifulsoup for XML使用 Beautifulsoup for XML 在一级获取标签名称
【发布时间】:2016-07-13 07:50:54
【问题描述】:

与此相关:Getting Tag Names with BeautifulSoup

我有一个 XML 文档,我想在一个级别上获取所有标签 - 所以解析

<parent>
    <child>
    </child>
</parent>  
<parent2>
</parent2> 

将返回['parent', 'parent2']。我怎样才能做到这一点?使用

for tag in document.findChildren():
    print tag.name

给予

['parent', 'child', 'parent2']

这是不受欢迎的。

【问题讨论】:

  • 使用.children
  • 我的错误,已编辑问题
  • .children 返回['None', 'Parent', 'None]

标签: python python-2.7 beautifulsoup bs4


【解决方案1】:

使用recursive 关键字

for tag in document.findChildren(recursive=False):
    print tag.name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多