Ian Bicking: the old part of his blog

Xml processing comment 000

AFAIK, BeautifulSoup structures aren't writable, which is what I'm doing -- parsing HTML, modifying the parsed form (adding comments), then writing it out again.

Comment on Re: XML Processing
by Ian Bicking

Comments:

You can change BeautifulSoup structures. For example, you can insert raw html fragments:

>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup('<html><body><p> text 1 <p> text 2 </html>')
>>> print soup
<html><body><p> text 1 </p><p> text 2 </p></body></html>
>>> par2 = soup('p')[1]
>>> par2.name = 'div'
>>> par2.contents = ['<p>'] + par2.contents + ['</p>']
>>> print soup
<html><body><p> text 1 </p><div><p> text 2 </p></div></body></html>
# Alexander Kozlovsky