December 2009
November 2009
October 2009
September 2009
June 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
July 2008
June 2008
October 2007
September 2007
In my post from a while back, I gave an example of the standard HTMLParser's use. HTMLParser is not the easiest way to glean information from HTML. There are two modules that are not part of the standard python distribution that can shorten the development time. The first is BeautifulSoup. Here is the code from the previous episode using BeautifulSoup instead of HTMLParser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/usr/bin/env python from BeautifulSoup import BeautifulSoup from mechanize import Browser br = Browser() response = br.open('http://tylerlesmann.com/') soup = BeautifulSoup(response.read()) headers = soup.findAll('div', attrs={'class': 'header'}) headlines = [] for header in headers: links = header.findAll('a') for link in links: headlines.append(link.string) for headline in headlines: print headline |
This is a lot shorter, 16 instead of 38 lines. It also took about 20 seconds to write. There is one gotcha here. Both scripts do the same task. This one using BeautifulSoup takes over twice as long to run. CPU time is much cheaper than development time though.
The next module is lxml. Here's the lxml version of the code.
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/env python from lxml.html import parse from mechanize import Browser br = Browser() response = br.open('http://tylerlesmann.com/') doc = parse(response).getroot() for link in doc.cssselect('div.header a'): print link.text_content() |
As you can see, it is even shorter than BeautifulSoup at 10 lines. On top of that, lxml is faster than HTMLParser. So what is the catch? The lxml module uses C code, so you will not be able to use it on Google's AppEngine or on Jython.
