【问题标题】:Print closing tags with Beautiful Soup用 Beautiful Soup 打印结束标签
【发布时间】:2012-02-10 18:51:08
【问题描述】:

这段代码打印出所有标签:

soup = BeautifulSoup.BeautifulSoup(html)
    for child in soup.recursiveChildGenerator():
    name = getattr(child, "name", None)
    if name is not None:
       print name

是否也可以打印结束标签?

例子:

<html>
    <body>
        <h1>My First Heading</h1>
        <p>My first paragraph.</p>
    </body>
</html>

打印:

html
body
h1
p

我希望它打印出来:

html
body
h1
/h1
p
/p
/body
/html

【问题讨论】:

    标签: python html tags beautifulsoup


    【解决方案1】:

    没有那种机制。您需要手动递归,在递归尝试后输出关闭标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多