views:

65

answers:

4

I am attempting to create an html document parser with Python. I am very familiar with jQuery and I would like to use its traversing functionality to parse these html files and return the data gathered with jQuery back to my Python program.

Is there any way to use javascript scripts through Python? Or is this just a pipe dream?

+1  A: 

jQuery itself does not contain an HTML/XML parser at all. It uses the browser to do all its parsing. Thus, even if you figure out how to run Javascript from Python, it won't do you any good.

Pointy
+5  A: 

You might not need to do this. There is a Python module called PyQuery that directly emulates the API for jQuery. It works exactly as you would expect it to in almost every way. Give it a shot!

jathanism
ah this appears to be what I need! thanks!
Awesome! Glad to help!
jathanism
+1  A: 

jQuery doesn't parse HTML - it traverses the DOM. You'd need an entire rendering engine (e.g. WebKit) if you wanted to use jQuery to work on the HTML.

Skilldrick
A: 

Well from your question it seems you will require python-javascript bridge like Pyjamas http://pyjs.org/ , PyPy http://codespeak.net/pypy/dist/pypy/doc/ , skulpt http://www.skulpt.org/ . Or my personal favorite PyXPCOM http://pyxpcomext.mozdev.org/ it installs a python backend directly into the firefox browser and using xpi stubs one can make bidirectioal calls ( mind you very complicated )

jknair
That would work in the wrong "direction": Pyjamas lets you compile python code to be run by a web browser or other javascript interpreter. The OP wants to run javascript code in a Python program.
intuited