BeautifulSoup: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
Beautiful Soup is a Python library for manipulating HTML pages.
Beautiful Soup is a Python library for manipulating HTML pages.


* [[http://www.crummy.com/software/BeautifulSoup/]]
* [http://www.crummy.com/software/BeautifulSoup/]
* [[http://www.crummy.com/software/BeautifulSoup/documentation.html documentation]]  
* [http://www.crummy.com/software/BeautifulSoup/documentation.html documentation]


== Code Examples ==
== Code Examples ==

Revision as of 11:43, 12 June 2008

Beautiful Soup is a Python library for manipulating HTML pages.

Code 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()