Вопрос

I am learning pyqt,use for parse webpage.

Now i want use pyqt evaluate javascript function just like this answer do: spidermonkey evaluate js function which in remote js file


import urllib2
import spidermonkey
js = spidermonkey.Runtime()
js_ctx = js.new_context()
script = urllib2.urlopen('http://etherhack.co.uk/hashing/whirlpool/js/whirlpool.js').read()
    js_ctx.eval_script(script)
    js_ctx.eval_script('var s = "abc"')
    js_ctx.eval_script('print(HexWhirlpool(s))')

I want know how to achieve the same effect by using pyqt instead of spidermonkey.

Это было полезно?

Решение

PyQt is a UI framework, so it might not be the best arrow in your quiver.

You can use WebView to load and display the web page. Since WebView uses WebKit, it will load and parse the HTML and all CSS and JavaScript in it. But that means you just get the final result - you don't have much control what is loaded and when.

Or you can use a tool like Beautiful Soup to parse the HTML. This gives you full control. You can then try to use spidermonkey to run the JavaScript but that will fail since many global variables like window or document will be missing. Also, there won't be a DOM.

For this, you'd need something like Envjs but there is no Python integration.

Maybe PhantomJS (a script driven Chrome/Chromium browser) is an option. See this question: Is there a way to use PhantomJS in Python?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top