简体   繁体   English

通过Python执行Javascript

[英]Javascript Execution Through Python

I am attempting to create an html document parser with Python. 我试图用Python创建一个html文档解析器。 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. 我对jQuery非常熟悉,我想使用它的遍历功能来解析这些html文件,并将使用jQuery收集的数据返回给我的Python程序。

Is there any way to use javascript scripts through Python? 有没有办法通过Python使用javascript脚本? Or is this just a pipe dream? 或者这只是一个梦想?

You might not need to do this. 您可能不需要这样做。 There is a Python module called PyQuery that directly emulates the API for jQuery. 有一个名为PyQuery的Python模块直接模拟jQuery的API。 It works exactly as you would expect it to in almost every way. 它几乎在所有方面都与您期望的完全一样。 Give it a shot! 试一试!

jQuery itself does not contain an HTML/XML parser at all. jQuery本身根本不包含HTML / XML解析器。 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. 因此,即使你弄清楚如何从Python运行Javascript,它对你没有任何好处。

jQuery doesn't parse HTML - it traverses the DOM. jQuery不解析HTML - 它遍历DOM。 You'd need an entire rendering engine (eg WebKit ) if you wanted to use jQuery to work on the HTML. 如果你想使用jQuery来处理HTML,你需要一个完整的渲染引擎(例如WebKit )。

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/ . 从您的问题来看,似乎您将需要python-javascript桥,如睡衣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 ) 或者我个人最喜欢的PyXPCOM http://pyxpcomext.mozdev.org/它将python后端直接安装到firefox浏览器中并使用xpi存根可以进行双向呼叫(请注意,你很复杂)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM