Parsing HTML

Jeff Rush jrush at
Fri Jun 18 09:04:55 CEST 1999

On Thu, 17 Jun 1999 13:27:33, Mordy Ovits <movits at> wrote:

> What is the best way to parse HTML into a Python data structure, allow me change it,
> and output it as HTML?
> -- 
> o Mordy Ovits
> o Cryptographic Engineer
> o LockStar Inc.

One good way is to use the DOM framework of XML.  You can get the module
and docs at in the XML-SIG section.  A tiny example follows:

----- cut here -----

#!/usr/bin/env python

from xml.dom.html_builder import HtmlBuilder
from xml.dom.writer       import HtmlWriter

# Read in Original HTML Source
# and Build a DOM Tree Structure
# ----------------------------

htmlstr = open('test.html', 'r').read()
b = HtmlBuilder(ignore_mismatched_end_tags=1)
b.feed(htmlstr)   # Stuff the HTML Source into the HTML Parser
doc = b.document  # Get the Newly Constructed Document Object

# Perform Modifications as Needed
# -------------------------------

text = doc.createTextNode("Additional Title Text")
titlenode = doc.getElementsByTagName('TITLE')[0]

# Write DOM Tree Back as HTML Source
# --------------------------------

fd = open('output.html', 'w')
w = HtmlWriter(stream=fd)

----- cut here -----

There is also an XML/DOM based HTML Pretty Printer posted
in here a week ago, which I find useful to clean up the output
HTML.  If you can't find it, drop me a line.

-Jeff Rush

More information about the Python-list mailing list