Skip to content

Instantly share code, notes, and snippets.

@datahutrepo
Last active September 7, 2016 06:33
Show Gist options
  • Save datahutrepo/e4f3e55ab9fe059cc4e30171ab6ac4a9 to your computer and use it in GitHub Desktop.
Save datahutrepo/e4f3e55ab9fe059cc4e30171ab6ac4a9 to your computer and use it in GitHub Desktop.
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 [16]: print etree.tostring(root, pretty_print=True)
<html>
<head>
<title>lxml Example</title>
</head>
<body>
<h2>Learning to lxml, a XML toolkit library in python</h2>
</body>
</html>
In [17]: h2.set('style', 'color: red')
In [18]: print etree.tostring(root, pretty_print=True)
<html>
<head>
<title>lxml Example</title>
</head>
<body>
<h2 style="color: red">Learning to lxml, a XML toolkit library in python</h2>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment