Parse HTML via XPath
Question: In .Net, I found this great library,
[HtmlAgilityPack](http://www.codeplex.com/htmlagilitypack) that allows you to
easily parse non-well-formed HTML using XPath. I've used this for a couple
years in my .Net sites, but I've had to settle for more painful libraries for
my Python, Ruby and other projects. Is anyone aware of similar libraries for
other languages?


Answer: I'm surprised there isn't a single mention of lxml. It's blazingly fast and
will work in any environment that allows CPython libraries.

Here's how [you can parse HTML via XPATH using
lxml](http://codespeak.net/lxml/xpathxslt.html).

    
    
    >>> from lxml import etree
    >>> doc = '<foo><bar></bar></foo>'
    >>> tree = etree.HTML(doc)
    
    >>> r = tree.xpath('/foo/bar')
    >>> len(r)
    1
    >>> r[0].tag
    'bar'
    
    >>> r = tree.xpath('bar')
    >>> r[0].tag
    'bar'
    

