replacing words in HTML file

Walter Dörwald walter at livinglogic.de
Fri Apr 30 05:27:41 EDT 2010


On 28.04.10 15:02, james_027 wrote:
> hi,
> 
> Any idea how I can replace words in a html file? Meaning only the
> content will get replace while the html tags, javascript, & css are
> remain untouch.

You could try XIST (http://www.livinglogic.de/Python/xist/):

Example code:

from ll.xist import xsc, parsers

def p2p(node, converter):
    if isinstance(node, xsc.Text):
        node = node.replace("Python", "Parrot")
        node = node.replace("python", "parrot")
    return node

node = parsers.parseurl("http://www.python.org/", tidy=True)

node = node.mapped(p2p)
node.write(open("parrot_index.html", "wb"))


Hope that helps!

Servus,
   Walter



More information about the Python-list mailing list