2012-07-04 11 views
1

我使用PyQt4的QtWebKit來渲染內存中的網頁,因爲我需要執行JavaScript,因爲我需要檢索嵌入式Flash視頻元素。目前我使用看起來像這樣的代碼:使用QtWebKit在內存中渲染多個網頁會在第二頁中斷,爲什麼?

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from PyQt4.QtWebKit import QWebSettings, QWebPage 

class Render(QWebPage): 
    def __init__(self, url): 
     self.app = QApplication(sys.argv) 
     QWebPage.__init__(self) 

     # Settings 
     s = self.settings() 
     s.setAttribute(QWebSettings.AutoLoadImages, False) 
     s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False) 
     s.setAttribute(QWebSettings.PluginsEnabled, True) 

     self.loadFinished.connect(self._loadFinished) 
     self.mainFrame().load(QUrl(url)) 
     self.app.exec_() 

    def _loadFinished(self, result): 
     self.frame = self.mainFrame() 
     self.app.quit() 

def get_page_source(url): 
    r = Render(url) 
    html = r.frame.toHtml() 
    return html 

現在這個工程確定,但它是初始化(服用任何地方5-30秒之間展開)非常慢,但是它只能確定單個頁。這意味着第一個網頁,我的最終輸出結果如下:

<div> 
    <embed type="application/x-shockwave-flash" src="/player.swf" width="560" height="440" style="undefined" id="mediaplayer" name="mediaplayer" quality="high" allowfullscreen="true" wmode="opaque" flashvars="width=560&amp;height=440&amp;autostart=true&amp;fullscreen=true&amp;file=FILELINK"></embed> 
</div> 

但在連續嘗試,它看起來像這樣:

<div> 
    <font> 
     <u> 
      <b> 
       <a href="http://get.adobe.com/flashplayer/">ATTENTION:<br>This video will not play. You currently do not have Adobe Flash installed on this computer. Please click here to download it (it's free!) 
       </a> 
      </b> 
     </u> 
    </font> 
</div> 

正在發生的事情在這裏,我不知道的?

+0

究竟是你想檢索 - 只是 '文件= FILELINK'? –

+0

@HughBothwell是的,但這不是問題。我需要知道爲什麼它會在連續嘗試檢索頁面後中斷。 – Atheuz

回答

1

它看起來像你的JavaScript解釋器只在第一頁上踢;第二頁加載,但從來沒有得到它的JavaScript運行;但是這無關你真正的問題,這是視頻文件的名稱是隱藏的代碼塊,看起來像

<script type="text/javascript"> 
    var googleCode = 'czEuYWRkVmFyaWFibGUoImZpbGUiLCJodHRwOi8vd2lsbGlhbS5yaWtlci53aW1wLmNvbS9sb2FkdmlkZW8vMDA5YzUwMzNkZmYyMDQ3MmJiYzBjMjk2NmJjNzI2MjIvNGZmNGQ2ZDYvd2ViLXZpZGVvcy9iZTVjYWI2YjcxNmU0OWExZjFiYzc3NGNlMjVlZDg0Yl93YWtlci5mbHYiKTs='; 
    eval(lxUTILsign.decode(googleCode)); 
</script> 

如果你調用一個JavaScript控制檯,並運行lxUTILsign.decode(googleCode);

"s1.addVariable(\"file\",\"http://worf.wimp.com/loadvideo/2e368b70f8577ad167087530fc73748d/4ff4f5df/web-videos/35e78d1932b24f80ae3a9210fce008c4_titanic.flv\");" 

不好消息是lxUTILsign被徹底混淆了; 好的消息是,這是無關緊要的,因爲它只是一個base64解碼器,而Python已經有一個(包括電池,寶貝!)。

import base64 
import urllib2 
import re 

def get_video_url(page_url): 
    html = urllib2.urlopen(url).read() 
    match = re.search("googleCode = '(.*?)'", html) 
    if match is None: 
     raise ValueError('googleCode not found') 
    googleString = base64.b64decode(match.group(1)) 
    match = re.search('","(.*?)"', googleString) 
    if match is None: 
     raise ValueError("didn't find video url") 
    return match.group(1) 

url = 'http://www.wimp.com/titanicpiano/' 
print get_video_url(url) 

回報

http://worf.wimp.com/loadvideo/8656607f77689f759d54b4ec7207152d/4ff4ff9c/web-videos/35e78d1932b24f80ae3a9210fce008c4_titanic.flv 
+0

Welp,這是實現它的一種方式,比實際渲染頁面快得多。我不知道它是從哪裏得到的。謝謝一堆。 – Atheuz

相關問題