[英]How can I add unescaped text to an LXML Etree in Python?
LXML's builder allows for easily generation of HTML and XML , like this: LXML 的构建器允许轻松生成 HTML 和 XML ,如下所示:
>>>from lxml.builder import E
>>>import lxml.etree
>>>lxml.etree.tostring(E.html('hello'))
b'<html>hello</html>'
But if I include text that's already in HTML, it escapes the angle brackets, as it should:但是,如果我包含 HTML 中已经存在的文本,它将转义尖括号,因为它应该:
>>>lxml.etree.tostring(E.html('<b>Hello</b>'))
b'<html><b>Hello</b></html>'
So how can I get it to treat the inner text as raw HTML/XML?那么我怎样才能让它将内部文本视为原始 HTML/XML 呢? I'd like to get it to output
<html><b>Hello</b></html
in the above example.在上面的示例中,我想将它发送到 output
<html><b>Hello</b></html
。
You can easily accomplish this by parsing your html string into an lxml etree object:您可以通过将 html 字符串解析为 lxml etree object 轻松完成此操作:
In [1]: from lxml.builder import E
In [2]: import lxml.etree
In [3]: lxml.etree.tostring(E.html(lxml.etree.fromstring('<b>Hello</b>')
Out[3]: b'<html><b>Hello</b></html>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.