简体   繁体   中英

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:

>>>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:

>>>lxml.etree.tostring(E.html('<b>Hello</b>'))                                                       
b'<html>&lt;b&gt;Hello&lt;/b&gt;</html>'

So how can I get it to treat the inner text as raw HTML/XML? I'd like to get it to output <html><b>Hello</b></html in the above example.

You can easily accomplish this by parsing your html string into an 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>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM