我正在使用 BeautifulSoup4,我很好奇是否有一个函数可以返回 HTML 代码的结构(有序标签)。
这是一个例子:
<html>
<body>
<h1>Simple example</h1>
<p>This is a simple example of html page</p>
</body>
</html>
print 页面.结构():
>>
<html>
<body>
<h1></h1>
<p></p>
</body>
</html>
我试图找到解决方案但没有成功。
Thanks
据我所知,没有,但一点递归应该起作用:
def taggify(soup):
for tag in soup:
if isinstance(tag, bs4.Tag):
yield '<{}>{}</{}>'.format(tag.name,''.join(taggify(tag)),tag.name)
demo:
html = '''<html>
<body>
<h1>Simple example</h1>
<p>This is a simple example of html page</p>
</body>
</html>'''
soup = BeautifulSoup(html)
''.join(taggify(soup))
Out[34]: '<html><body><h1></h1><p></p></body></html>'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)