Use of HTMLparser to change language

Vlastimil Brom vlastimil.brom at
Fri Mar 20 13:42:08 CET 2009

2009/3/20 pranav <pranny at>:
> Greetings All,
> I have huge number of HTML files, all in english. I also have their
> counterpart files in Spanish. The non english files have their look
> and feel a little different than their english counterpart.
> My task is to make sure that the English HTML files contain the
> Spanish text, with retaining the English look and feel.
> Pranny
> --

Hi, I guess, this task can probably not be solved fully automatically
unless there is some exact structure of the HTML, but it doesn't seem
If you would prefer to work with static sources, you can try to
identify the differences in the markup of english and spanish pages.

e.g. using BeautifulSoup
or at least approximately with regular expressions,
tags_only_source = re.findall(r"<[^>]+>", html_source)
should return the tags source for simple code (neglecting nesting,
commented code, strings containing tags source ...)

the difflib library then could help in identifying the differences in code, cf:

>>> for difference in difflib.ndiff("abcadefsdf", "abQcadsdfAA"): print difference
+ Q
- e
- f
+ A
+ A

(sample strings used here as arguments for ndiff can also be lists of
strings returned by findall() above.)

If you are lucky and the differences are rather small and regular, you
can then try to modify the markup in the spanish pages to be more
similar to the english ones;
again possibly using BeautifulSoup or even re.sub(...)
(of course, saving the modified sources as new files in some other directory)
(The opposite - taking the english markup and feeding it with english
text - would be more tricky, I guess.)

However, all that is likely to help only with the part of the task,
which will almost certainly require, more or less "manual" work.
Someone more experienced can probably propose a more effective


More information about the Python-list mailing list