From: Cameron Simpson on
On 30Apr2010 07:15, Stefan Behnel <stefan_ml(a)behnel.de> wrote:
| Cameron Simpson, 30.04.2010 00:47:
| >Here's a function from a script I wrote to bulk edit a web site. I was
| >replacing OBJECT and EMBED nodes with modern versions:
| >
| > def recurse(node):
| > global didmod
| > [...]
| > didmod=True
| > continue
| > recurse(O)
| >
| >The calling end looks like this:
| >
| > SOUP = BeautifulSoup(srctext)
| > didmod = False # icky global set by recurse()
| > recurse(SOUP)
| > if didmod:
| > srctext = str(SOUP)
| >
| >If didmod becomes True we recompute srctext and resave the file (or save it
| >to a copy).
|
| You should rethink your naming in the above code and remove the need
| for a global variable.

Absolutely. It was a quick hack, not something I was intending anyone
else to use.
--
Cameron Simpson <cs(a)zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

[I]n the current environment, it's really hard to ascribe to stupidity
what can be adequately explained by malice. - Jordin Kare
From: Walter Dörwald on
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