Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery-like HTML parsing in Python?

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 selectors 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

like image 251
Roy Tang Avatar asked Jun 16 '10 07:06

Roy Tang


1 Answers

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

Usage:

from bs4 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>,     ..] 
like image 50
systempuntoout Avatar answered Sep 21 '22 00:09

systempuntoout