使用QtWebKit在内存中渲染多个网页在第二页中断,为什么?

时间:2012-07-04 23:16:33

标签: python pyqt4 qtwebkit

我正在使用PyQt4的QtWebKit来渲染内存中的网页,因为我需要执行javascript,因为我需要检索嵌入式Flash视频元素。目前我正在使用的代码如下所示:

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>

这里发生了什么,我不知道?

1 个答案:

答案 0 :(得分: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