2017 m. rugsėjo 18 d., pirmadienis

python lxml

>> root = etree.Element("root")
>> etree.tostring(root)


Create html document:
>>> html = etree.Element("html")
>>> body = etree.SubElement(html, "body")
>>> body.text = "Added text" # tail adds text after tag
>>> etree.tostring(html)
b'<html><body>Added text</body></html>'

Creating elements with text elements
>>> root = etree.Element("root") 
>>> etree.SubElement(root, "child").text = "Child 1" 
>>> etree.SubElement(root, "child").text = "Child 2" 
>>> etree.SubElement(root, "another").text = "Child 3"


Display only textual data:
>>> etree.tostring(html, method="text")
>>> print(html.xpath("string()")) 
>>> print(html.xpath("//text()")) #Display textual data as list


Iteration:
>>> [(x.tag, x.text) for x in root.iter()]
> [ child.tag for child in root ] # root child element itteration

Finding elements: 
>> print(root.find(".//b").tag)
>>> [ b.tag for b in root.iterfind(".//b") ] # Finding multiple elements

Dumping xml:
xml.etree.ElementTree.dump(tree)



http://lxml.de/index.html - Official website.
http://lxml.de/3.8/lxmldoc-3.8.0.pdf - Documentation