BeautifulSoup

From XPUB & Lens-Based wiki
Revision as of 11:41, 12 June 2008 by Michael Murtaugh (talk | contribs) (New page: Beautiful Soup is a Python library for manipulating HTML pages. !! Examples A function to replace the contents of a tag: <source lang="python"> import BeautifulSoup soup = BeautifulSoup....)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Beautiful Soup is a Python library for manipulating HTML pages.

!! Examples

A function to replace the contents of a tag:

import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup("<ul><li>one</li><li>two</li></ul>")

def setcontents (tag, val):
	# remove previous contents
	for c in tag.contents:
		c.extract()
	# insert the new
	tag.insert(0, val)

items = soup.findAll("li")
for item in items:
	setcontents(item, "foo")

print soup.prettify()