2013-05-11 89 views
1

我正在學習pyqt,用於解析網頁。如何使用pyqt評估javascript

現在我想用PyQt的評估JavaScript函數就像這個回答做到: 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))') 

我想知道如何使用PyQt的SpiderMonkey的,而不是來達到同樣的效果。

+0

請更具體地說明你準備實現的是什麼,如果可能的話,在你的文章中添加一些示例代碼 – 2013-05-13 10:23:52

+0

感謝你的建議 – Grep 2013-05-13 12:14:23

回答

0

PyQt是一個UI框架,所以它可能不是你箭袋中最好的箭頭。

您可以使用WebView加載和顯示網頁。由於WebView使用WebKit,因此它將加載和解析HTML以及其中的所有CSS和JavaScript。但這意味着你只能得到最終結果 - 你沒有太多的控制什麼是加載和什麼時候。

或者您可以使用像Beautiful Soup這樣的工具來解析HTML。這給你完全的控制。然後,您可以嘗試使用spidermonkey來運行JavaScript,但由於許多全局變量(如windowdocument)將會丟失,因此會失敗。另外,不會有DOM。

爲此,您需要類似Envjs但是there is no Python integration

也許PhantomJS(腳本驅動的Chrome/Chromium瀏覽器)是一個選項。看到這個問題:Is there a way to use PhantomJS in Python?

+0

非常感謝,它是對我很有幫助。 – Grep 2013-05-14 05:03:34