是否有办法(使用python和lxml)获取HTML代码的输出:
<table class=main>
<tr class=row>
</tr>
</table>
而不是像这样的人:
<table class=main><tr class=row></tr>
</table>
只能附加div标签中名为“span”的标签。所以像:
<div class=paragraph><span class=font48>hello</span></div>
是允许的。 非常感谢您的帮助。
答案 0 :(得分:2)
你可以在每个“&lt;”之前插入换行符正则表达式
答案 1 :(得分:2)
另一种选择是使用BeautifulSoup:
from bs4 import BeautifulSoup
html = "<table class=main><tr class=row></tr></table>"
soup = BeautifulSoup(html)
print soup.prettify()
输出:
<table class="main">
<tr class="row">
</tr>
</table>
答案 2 :(得分:0)
您是否考虑过模块prettify()
中的BeautifulSoup方法?
#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup as bs
html = '<table class=main><tr class=row></tr>\
</table>'
print bs(html).prettify()
输出:
<table class="main">
<tr class="row">
</tr>
</table>
注意 - 它会为输出添加一些缩进,如您所见。