我正在使用 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&height=440&autostart=true&fullscreen=true&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>
这里发生了什么我不知道的事情?