replacing words in HTML file

J

james_027

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.

THanks,
James
 
D

Daniel Fetchinson

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.

I'm not sure what you tried and what you haven't but as a first trial
you might want to

<untested>

f = open( 'new.html', 'w' )
f.write( open( 'index.html' ).read( ).replace( 'replace-this', 'with-that' ) )
f.close( )

</untested>

HTH,
Daniel
 
L

Luap777

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.

THanks,
James

You might try cleaning the HTML with uTidy (http://
utidylib.berlios.de/) to make XHTML then using Beautiful Soup (http://
www.crummy.com/software/BeautifulSoup/documentation.html) to process
it.

If the number of files isn't that large and it's a one-time thing, you
might be just as well using search and replace on the directory and
previewing each replacement as you go....
 
W

Walter Dörwald

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top