Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'tree': 0.05; 'context': 0.07; 'element': 0.07; 'nicely': 0.07; 'wednesday,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'structure,': 0.09; 'way:': 0.09; 'assume': 0.14; 'both,': 0.16; 'earlier.': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'generate,': 0.16; 'generates:': 0.16; 'looking?': 0.16; 'nodes.': 0.16; 'obviously,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; '(you': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'mechanism': 0.19; 'stefan': 0.19; 'seems': 0.21; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'text.': 0.24; 'decide': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'leave': 0.29; 'tim': 0.29; "i'm": 0.30; '>>>>': 0.31; 'doc': 0.31; 'dom': 0.31; 'piece': 0.31; 'txt': 0.31; 'allows': 0.31; 'this.': 0.32; 'option': 0.32; 'table': 0.34; 'skip:d 20': 0.34; 'subject:with': 0.35; "can't": 0.35; 'etc': 0.35; 'received:84': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'method': 0.36; "i'll": 0.36; 'should': 0.36; 'skip:o 20': 0.38; 'manager': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'moving': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'most': 0.60; 'new': 0.61; 'believe': 0.68; '20,': 0.68; 'approach.': 0.91; 'hot': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Working with HTML5 documents Date: Thu, 20 Nov 2014 20:02:06 +0100 References: <69c8db11-5928-4978-a0ca-0cd042f0febb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-053-082.084.056.pools.vodafone-ip.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <69c8db11-5928-4978-a0ca-0cd042f0febb@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416510142 news.xs4all.nl 2891 [2001:888:2000:d::a6]:40386 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81107 Tim schrieb am 20.11.2014 um 18:31: > On Thursday, November 20, 2014 12:04:09 PM UTC-5, Denis McMahon wrote: >>> On Wednesday, November 19, 2014 2:08:27 PM UTC-7, Denis McMahon wrote: >>>> So what I'm looking for is a method to create an html5 document using >>>> "dom manipulation", ie: >>>> >>>> doc = new htmldocument(doctype="HTML") >>>> html = new html5element("html") >>>> doc.appendChild(html) >>>> head = new html5element("body") >>>> html.appendChild(head) >>>> body = new html5element("body") >>>> html.appendChild(body) >>>> title = new html5element("title") >>>> txt = new textnode("This Is The Title") >>>> title.appendChild(txt) >>>> head.appendChild(title) >>>> para = new html5element("p") >>>> txt = new textnode("This is some text.") >>>> para.appendChild(txt) >>>> body.appendChild(para) >>>> >>>> print(doc.serialise()) >>>> >>>> generates: >>>> >>>> This Is The Title>>> head>

This is some text.

>>>> >>>> I'm finding various mechanisms to generate the structure from an >>>> existing piece of html (eg html5lib, beautifulsoup etc) but I can't >>>> seem to find any mechanism to generate, manipulate and produce html5 >>>> documents using this dom manipulation approach. Where should I be >>>> looking? >> >> Everything there seems to assume I'll be creating a document serially, eg >> that I won't get to some point in the document and decide that I want to >> add an element earlier. >> >> bs4 and html5lib will parse a document into a tree structure, but they're >> not so hot on manipulating the tree structure, eg adding and moving nodes. >> >> Actually it looks like bs4 is going to be my best bet, although limited >> it does have most of what I'm looking for. I just need to start by giving >> it "" to parse. > > I believe lxml should work for this. Here's a snippet that I have used to create an HTML document: > > from lxml import etree > page = etree.Element('html') > doc = etree.ElementTree(page) > > head = etree.SubElement(page, 'head') > body = etree.SubElement(page, 'body') > table = etree.SubElement(body, 'table') > > etc etc > > with open('mynewfile.html', 'wb') as f: > doc.write(f, pretty_print=True, method='html') > > (you can leave out the method= option to get xhtml). There's also the E-factory for creating (sub-)trees and a nicely objectish way: http://lxml.de/lxmlhtml.html#creating-html-with-the-e-factory and the just released lxml 3.4.1 has an "htmlfile" context manager that allows you to generate HTML incrementally: http://lxml.de/api.html#incremental-xml-generation Obviously, you can combine both, so you can create a subtree in memory and write it into an incrementally built HTML stream. Pretty versatile. Stefan