【问题标题】:Python QtWebKit save webpage to filePython QtWebKit 将网页保存到文件
【发布时间】:2011-09-15 15:14:53
【问题描述】:

将使用 QWebView() 显示的网页保存到文件的最佳和最简单的方法是什么?

from PyQt4.QtCore import *
from PyQt4.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_())

【问题讨论】:

    标签: python pyqt4 qtwebkit


    【解决方案1】:

    由于页面加载是异步的,您必须等待loadFinished 信号才能尝试保存。

    然后您可以使用 web.page().currentFrame().toHtml() 检索页面内容,它返回一个 python unicode 字符串,您可以使用 codecs 模块将其写入文件:

    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_())
    

    【讨论】:

    • 非常感谢! ;) 还有一个简单的问题:迭代多个链接的最佳方法是什么?
    • save()函数的末尾,你可以简单地更改currentfile中的文件名,而不是app.quit(),然后用下一个url调用web.load()
    • 这就是我试图做的,但它并没有真正等待第一个正确加载并在第一个 URL 上生成下载错误......
    • @Cat 我更新了代码以允许下载多个链接。您得到的错误可能来自与变量范围相关的问题。
    • alexisdm,有什么方法可以等待 javascript 完成加载,而不仅仅是页面?它仍然只保存基本页面...
    【解决方案2】:

    页面需要先加载 QtWebKit 有什么原因吗?只需使用命令行实用程序 wget 或 curl,就可以完成这项工作。

    【讨论】:

      猜你喜欢
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2013-09-01
      • 2014-03-01
      相关资源
      最近更新 更多