使用 Beautiful Soup 打印结束标签
此代码打印出所有标签:
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
This code prints out all tags:
soup = BeautifulSoup.BeautifulSoup(html)
for child in soup.recursiveChildGenerator():
name = getattr(child, "name", None)
if name is not None:
print name
Is it possible to print out closing tags too?
Example:
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Prints:
html
body
h1
p
I want it to print:
html
body
h1
/h1
p
/p
/body
/html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有那个机制。您需要手动递归,在递归尝试后输出关闭标记。
Not with that mechanism. You'll need to recurse manually, outputting the close tag after the recursion attempt.