elementtree question
Stefan Behnel
stefan.behnel-n05pAM at web.de
Mon Sep 24 04:04:22 EDT 2007
Tim Arnold wrote:
> Hi, I'm using elementtree and elementtidy to work with some HTML files. For
> some of these files I need to enclose the body content in a new div tag,
> like this:
> <body>
> <div class="remapped">
> original contents...
> </div>
> </body>
Give lxml.etree (or lxml.html) a try:
tree = etree.parse("http://url.to/some.html", etree.HTMLParser())
body = tree.find("body")
and then:
div = etree.Element("div", {"class" : "remapped"})
div.extend(body)
body.append(div)
or alternatively:
children = list(body)
div = etree.SubElement(body, "div", {"class" : "remapped"})
div.extend(children)
http://codespeak.net/lxml/
and for lxml.html, which is currently in alpha status:
http://codespeak.net/lxml/dev/
ET 1.3 will also support the extend() function, BTW.
Stefan
More information about the Python-list
mailing list