如何使用缩进将HTML漂亮地打印到文件 [英] How to Pretty Print HTML to a file, with indentation

查看:95
本文介绍了如何使用缩进将HTML漂亮地打印到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 lxml.html 生成一些HTML.我想将最终结果漂亮地打印(带有缩进)到html文件中.我该怎么办?

I am using lxml.html to generate some HTML. I want to pretty print (with indentation) my final result into an html file. How do I do that?

这是我到目前为止一直在尝试的内容(我对Python和lxml还是比较陌生的):

This is what I have tried and got till now (I am relatively new to Python and lxml) :

import lxml.html as lh
from lxml.html import builder as E
sliderRoot=lh.Element("div", E.CLASS("scroll"), style="overflow-x: hidden; overflow-y: hidden;")
scrollContainer=lh.Element("div", E.CLASS("scrollContainer"), style="width: 4340px;")
sliderRoot.append(scrollContainer)
print lh.tostring(sliderRoot, pretty_print = True, method="html")

如您所见,我正在使用pretty_print=True属性.我以为可以缩进代码,但这并没有真正的帮助.这是输出:

As you can see I am using the pretty_print=True attribute. I thought that would give indented code, but it doesn't really help. This is the output :

<div style="overflow-x: hidden; overflow-y: hidden;" class="scroll"><div style="width: 4340px;" class="scrollContainer"></div></div>

推荐答案

我最终使用了 BeautifulSoup 直接.这就是 lxml.html.soupparser 用于解析HTML的东西.

I ended up using BeautifulSoup directly. That is something lxml.html.soupparser uses for parsing HTML.

BeautifulSoup有一个美化方法,可以完全按照其说的做.它使用适当的缩进和所有内容来修饰HTML.

BeautifulSoup has a prettify method that does exactly what it says it does. It prettifies the HTML with proper indents and everything.

BeautifulSoup不会修复HTML,因此损坏的代码仍然损坏.但是在这种情况下,由于代码是由lxml生成的,因此HTML代码至少在语义上应该正确.

BeautifulSoup will NOT fix the HTML, so broken code, remains broken. But in this case, since the code is being generated by lxml, the HTML code should be at least semantically correct.

在我的问题中给出的示例中,我将必须这样做:

In the example given in my question, I will have to do this :

from BeautifulSoup import BeautifulSoup as bs
root = lh.tostring(sliderRoot) #convert the generated HTML to a string
soup = bs(root)                #make BeautifulSoup
prettyHTML = soup.prettify()   #prettify the html

这篇关于如何使用缩进将HTML漂亮地打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆