开发者

Python QtWebKit save webpage to file

开发者 https://www.devze.com 2023-04-04 22:32 出处:网络
What\'s the best and simplest way to save a webpage displayed with QWebView() to file? from PyQt4.QtCore import *

What's the best and simplest way to save a webpage displayed with QWebView() to file?

from PyQt4.QtCore import *
from Py开发者_Python百科Qt4.QtWebKit import *
from PyQt4.QtGui import *
from PyQt4.QtScript import *
import sys
import time

currentfile = "test.htm"
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://news.google.com"))
web.show()
data =  web.page().currentFrame().documentElement().toInnerXml()
open(currentfile,"w").write(data)
sys.exit(app.exec_())


As the page loading is asynchronous, you have to wait for the loadFinished signal before trying to save it.

Then you can retrieve the page content with web.page().currentFrame().toHtml() which returns a python unicode string, which you can write to a file with the codecs module:

from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
import sys
import codecs

class Downloader(QObject):
    # To be emitted when every items are downloaded
    done = Signal()

    def __init__(self, urlList, parent = None):
        super(Downloader, self).__init__(parent)
        self.urlList = urlList
        self.counter = 0        
        # As you probably don't need to display the page
        # you can use QWebPage instead of QWebView
        self.page = QWebPage(self)      
        self.page.loadFinished.connect(self.save)
        self.startNext()

    def currentUrl(self):
        return self.urlList[self.counter][0]

    def currentFilename(self):
        return self.urlList[self.counter][1]

    def startNext(self):
        print "Downloading %s..."%self.currentUrl()
        self.page.mainFrame().load(self.currentUrl())

    def save(self, ok):
        if ok:            
            data = self.page.mainFrame().toHtml()
            with codecs.open(self.currentFilename(), encoding="utf-8", mode="w") as f:
                f.write(data)
            print "Saving %s to %s."%(self.currentUrl(), self.currentFilename())            
        else:
            print "Error while downloading %s\nSkipping."%self.currentUrl()
        self.counter += 1
        if self.counter < len(self.urlList):            
            self.startNext()
        else:
            self.done.emit()

urlList = [("http://news.google.com", "google.html"), 
    ("http://www.stackoverflow.com","stack.html"), 
    ("http://www.imdb.com", "imdb.html")]

app = QApplication(sys.argv)
downloader = Downloader(urlList)
# Quit when done
downloader.done.connect(app.quit)

# To view the pages
web = QWebView()
# To prevent user action that would interrupt the current page loading
web.setDisabled(True) 
web.setPage(downloader.page)
web.show()

sys.exit(app.exec_())


Is there a reason that the page needs to be loaded with QtWebKit first? Simply using the command-line utility wget, or curl, would do the job.

0

精彩评论

暂无评论...
验证码 换一张
取 消