我的印象是,使用PyQT实现webkit的无头浏览器将自动获得每个URL的html代码,即使其中包含大量JS代码。但我只看到了一部分。我正在与从firefox窗口保存页面时得到的页面进行比较。在
我正在使用以下代码-
class JabbaWebkit(QWebPage):
# 'html' is a class variable
def __init__(self, url, wait, app, parent=None):
super(JabbaWebkit, self).__init__(parent)
JabbaWebkit.html = ''
if wait:
QTimer.singleShot(wait * SEC, app.quit)
else:
self.loadFinished.connect(app.quit)
self.mainFrame().load(QUrl(url))
def save(self):
JabbaWebkit.html = self.mainFrame().toHtml()
def userAgentForUrl(self, url):
return USER_AGENT
def get_page(url, wait=None):
# here is the trick how to call it several times
app = QApplication.instance() # checks if QApplication already exists
if not app: # create QApplication if it doesnt exist
app = QApplication(sys.argv)
#
form = JabbaWebkit(url, wait, app)
app.aboutToQuit.connect(form.save)
app.exec_()
return JabbaWebkit.html
有人能看出代码有什么明显的错误吗?在
在通过几个url运行代码之后,我发现了一个非常清楚地显示了我遇到的问题-http://www.chilis.com/EN/Pages/menu.aspx
谢谢你的指点。在
页面有ajax代码,当它完成加载后,仍然需要一些时间来用ajax更新页面。但是你的代码会在它完成加载后退出。在
您应该添加一些类似这样的代码,以便在webkit中等待一些时间并处理事件:
相关问题 更多 >
编程相关推荐