from lxml import etree root = etree.XML('<root><a><b/></a></root>') print(etree.tostring(root)) #輸出:b'<root><a><b/></a></root>' print(etree.tostring(root, xml_declaration=True)) #輸出: <?xml version='1.0' encoding='ASCII'?> # <root><a><b/></a></root> print(etree.tostring(root, encoding='iso-8859-1')) #輸出: <?xml version='1.0' encoding='iso-8859-1'?> # <root><a><b/></a></root> print(etree.tostring(root, pretty_print=True)) '''輸出: <root> <a> <b/> </a> </root> ''' root = etree.XML('<html><head/><body><p>Hello<br/>World</p></body></html>') print(etree.tostring(root)) #輸出: b'<html><head/><body><p>Hello<br/>World</p></body></html>' print(etree.tostring(root, method='xml')) #輸出: b'<html><head/><body><p>Hello<br/>World</p></body></html>' print(etree.tostring(root, method='html')) #輸出: b'<html><head></head><body><p>Hello<br>World</p></body></html>' print(etree.tostring(root, method='html', pretty_print=True)) '''輸出: <html> <head></head> <body><p>Hello<br>World</p></body> </html> ''' print(etree.tostring(root, method='text')) #輸出: b'HelloWorld' br = next(root.iter('br')) br.tail = u'W\xf6rld' etree.tostring(root, method='text') #輸出: 報錯,由於編碼默認是ascii,而不是unicode print(etree.tostring(root, encoding='unicode', method='text')) #輸出:HelloWörld
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------html
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------編碼
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------spa