使用 PyQt4 返回 Javascript 生成的 HTML [英] Using PyQt4 to return Javascript generated HTML
问题描述
我正在使用以下样板 PyQt4 代码,目的是捕获页面上由 javascript 生成的所有 HTML:
I'm using the below boilerplate PyQt4 code with the goal of capturing all the HTML generated by javascript on a page:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
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 getHtml(str_url):
r_html = Render(str_url)
html = r_html.frame.toHtml()
return html
然后我创建了一个测试页面来查看它是否有效:
I then created a test page to see if it works:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').text('This is a test!')
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
就这么跑
getHtml('http://www.mytestpage.com')
我希望看到带有这是一个测试!"的 HTML在 div 中呈现的文本.然而,HTML 正在返回,但没有该部分.
I'd expect to see the HTML with the 'This is a test!' text rendered in the div. However the HTML is being returned with that piece absent.
我做错了什么?代码不是在等待页面完全加载吗?还是我误解了用例?
What am I doing wrong? Is the code not waiting for page to fully load? Or am I misunderstanding the use case?
推荐答案
问题是我的系统上没有正确安装 PyQt4.重新安装解决了这个问题.叹息
The issue was that I did not have PyQt4 properly installed on my system. Reinstalling fixed the issue. Sigh
这篇关于使用 PyQt4 返回 Javascript 生成的 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!