Take the 2-minute tour ×

Is there any Python library that allows me to parse an HTML document similar to what jQuery does?

i.e. I'd like to be able to use CSS selector syntax to grab an arbitrary set of nodes from the document, read their content/attributes, etc.

The only Python HTML parsing lib I've used before was BeautifulSoup, and even though it's fine I keep thinking it would be faster to do my parsing if I had jQuery syntax available. :D

share|improve this question

4 Answers 4

up vote 30 down vote accepted

If you are fluent with BeautifulSoup, you could just add soupselect to your libs.
Soupselect is a CSS selector extension for BeautifulSoup.


>>> from BeautifulSoup import BeautifulSoup as Soup
>>> from soupselect import select
>>> import urllib
>>> soup = Soup(urllib.urlopen('http://slashdot.org/'))
>>> select(soup, 'div.title h3')
[<h3><span><a href='//science.slashdot.org/'>Science</a>:</span></h3>,
 <h3><a href='//slashdot.org/articles/07/02/28/0120220.shtml'>Star Trek</h3>,
share|improve this answer
This sounds like the best solution for me right now, I'll give it a try. Thanks! –  Roy Tang Jun 16 '10 at 7:42
It's now from bs4 for Beautiful Soup 4 –  Andrew Jun 29 '13 at 1:15
In case you have problem to install soupselect, you should try the pip compatible version offered her github.com/syabro/soupselect : sudo pip install https://github.com/syabro/soupselect/archive/master.zip –  AsTeR Jan 22 at 17:59

Consider PyQuery:


>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq(url='http://google.com/')
>>> d = pq(url='http://google.com/', opener=lambda url: urllib.urlopen(url).read())
>>> d = pq(filename=path_to_html_file)
>>> d("#hello")
>>> p = d("#hello")
>>> p.html()
'Hello world !'
>>> p.html("you know <a href='http://python.org/'>Python</a> rocks")
>>> p.html()
u'you know <a href="http://python.org/">Python</a> rocks'
>>> p.text()
'you know Python rocks'
share|improve this answer

The lxml library supports CSS selectors.

share|improve this answer
Here's a simple example for lxml and CSS selectors –  Avision May 15 at 22:23

You might also want to have a look at pyquery. Find it here

share|improve this answer

Your Answer


