3

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?

2
  • Do you want to write the document parser as an exercise? If not, I see no reason why you wouldn't want to use the existing libraries. Commented Oct 6, 2010 at 15:46
  • As for "using JavaScript", I guess you mean to execute it. That means that you would have to write a JavaScript compiler/interpreter. Not an easy thing to do, but a totally different topic anyway. Commented Oct 6, 2010 at 15:48

4 Answers 4

6

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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 )

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.