Monday, December 8th, 2008

pyQuery: Same API, but with a snake.

Category: Python

jQuery came first, but then we have seen others such as pQuery.

Now we have pyQuery which brings the same great taste API to Python. The author Olivier Lauzanne explains:

yquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.

This is not (or at least not yet) a library to produce or interact with javascript code. I just liked the jquery API and I missed it in python so I told myself “Hey let’s make jquery in python”. This is the result.

It can be used for many purposes, one idea that I might try in the future is to use it for templating with pure http templates that you modify using pyquery.

You can use the PyQuery class to load an xml document from a string, a lxml document, from a file or from an url:

  1. >>> from pyquery import PyQuery
  2. >>> from lxml import etree
  3. >>> d = PyQuery("<html></html>")
  4. >>> d = PyQuery(etree.fromstring("<html></html>"))
  5. >>> d = PyQuery(url='')
  6. >>> d = PyQuery(filename=path_to_html_file)

We also saw new posts on pyjamas, the JS emitter (a la GWT for Java) that we covered awhile back.

Add to that the fantastic week that the pythonistas have had as they laud over us with Python 3000 having a final release (which will kick in updates to libraries that are needed to make this backwards not so compatible upgrade viable). It gives a kick in the backside to Perl, Ruby, Java, and even us JS folk :)

Posted by Dion Almaer at 7:45 am

3.3 rating from 40 votes


Comments feed TrackBack URI

That looks cool. I have not have a chance to play with lxml much, but I have with BeautifulSoup. It could be nice to have similar API’s for doing things on the server as in the browser.

Comment by webbm — December 8, 2008

Another good reason to write xhtml I guess. @webbm: I was thinking something similar… building this on top of beautifulsoup instead of lxml would have been truly beautiful.

Comment by newz2000 — December 8, 2008

Leave a comment

You must be logged in to post a comment.