views:

34

answers:

1

Why this code does not work ? I want save dom after js execute at this page and i want use qt without gui. Sorry for my English.

#coding:utf-8
from PyQt4 import QtCore, QtWebKit


class Sp():
    def save(self):
        print "call"
        data = self.webView.page().currentFrame().documentElement().toInnerXml()
        open("htm","w").write(data)
    def main(self):
        self.webView = QtWebKit.QWebPage()
        self.webView.load(QtCore.QUrl("http://www.google.com"))
        QtCore.QObject.connect(self.webView,QtCore.SIGNAL("loadFinished(bool)"),self.save)
s = Sp()
s.main()
+2  A: 

You have to create a QApplication before executing other stuffs.

Add this:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
s = Sp()
s.main()
sys.exit(app.exec_())

UPDATED: Also, change the code, because QWebPage doesn't have a load method:

import sys
from PyQt4 import QtGui, QtCore, QtWebKit

class Sp():
    def save(self):
        print "call"
        data = self.webView.page().currentFrame().documentElement().toInnerXml()
        open("htm","w").write(data)
        print 'finished'
    def main(self):
        self.webView = QtWebKit.QWebView()
        self.webView.load(QtCore.QUrl("http://www.google.com"))
        QtCore.QObject.connect(self.webView,QtCore.SIGNAL("loadFinished(bool)"),self.save)

app = QtGui.QApplication(sys.argv)
s = Sp()
s.main()
sys.exit(app.exec_())
rubik
thx, How i can open the html file locally? Without downloading
Artyom
self.webView.load(open("html.html").read()) not work
Artyom
rubik