Skip to content

Instantly share code, notes, and snippets.

In [8]: root = etree.Element('html')
In [9]: head = etree.SubElement(root, 'head')
In [10]: body = etree.SubElement(root, 'body')
In [11]: title = etree.SubElement(head, 'title')
In [12]: title.text = 'lxml Example'
In [13]: h2 = etree.SubElement(body, 'h2')
In [14]: h2.text = 'Learning to lxml, a XML toolkit library in python'
In [15]: print etree.tos
etree.tostring etree.tostringlist
In [5]: etree.SubElement(root, 'head')
Out[5]: <Element head at 0x7f43a5c51e60>
In [6]: etree.SubElement(root, 'body')
Out[6]: <Element body at 0x7f43a5c51f38>
In [7]: print etree.tostring(root)
<html><head/><body/></html>
@datahutrepo
datahutrepo / etree2
Last active September 7, 2016 06:31
In [2]: root = etree.Element('html')
In [3]: root
Out[3]: <Element html at 0x7f43a5c51ab8>
In [4]: print root.tag
html
Python 2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Dec 6 2015, 18:08:32)
Type "copyright", "credits" or "license" for more information.
IPython 4.1.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from lxml import etree