为什么 QNetworkReply readAll() 返回零字节?

发布于 2024-11-30 18:42:43 字数 1592 浏览 2 评论 0原文

我在 pyqt4 中使用 qtwebkit 通过 QNetworkReply 下载图像:

import os 
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

class dxBrowser(QWebPage):
def __init__(self,url):
    QWebPage.__init__(self)
    self._url=url
    self.manager=self.networkAccessManager()
    self.connect(self.manager,SIGNAL("finished(QNetworkReply *)"),self.onFinished)

def crawl(self):
    self.mainFrame().load(QUrl(self._url))

def onFinished(self,networkReply):
    if networkReply.rawHeader('Content-Type')=='image/png':
        print 'find the image'
        l=int(networkReply.rawHeader('Content-Length'))
        print l
        byteArray=networkReply.readAll()
        print byteArray.size()
        im=QImage.fromData(byteArray)
        if not im.save('test.jpg','jpg'):
            print 'image save error'


def main():
    app=QApplication(sys.argv)
    url='http://www.yiyaows.cn/DrsPath.do?kid=6666686E686E69673334333632303335&username=mylibddrz&spagenum=251&pages=50&fid=7534992&a=95cb07394dbf1d43c1fe61bdf6d4a36d&btime=2011-08-19&etime=2011-09-08&template=bookdsr1&firstdrs=http%3A%2F%2Fbook1.duxiu.com%2FbookDetail.jsp%3FdxNumber%3D000005609810%26d%3DA30222298F3C6715323B5476CB66D650'
    dx=dxBrowser(url)
    dx.crawl()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()    

虽然内容长度非零,但 byteArray.size() 为 0。所以我无法保存图像。为什么?谁能帮助我。

编辑: 也许我明白了这一点。 qtwebkit 可能已经检索了 qnetworkreply 的内容,一个 QIODevice,在 readall() 之后它的大小将为 0。我猜,也许 qtwebkit 作为浏览器已经读取它进行渲染。

I am using qtwebkit in pyqt4 to download images through QNetworkReply:

import os 
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

class dxBrowser(QWebPage):
def __init__(self,url):
    QWebPage.__init__(self)
    self._url=url
    self.manager=self.networkAccessManager()
    self.connect(self.manager,SIGNAL("finished(QNetworkReply *)"),self.onFinished)

def crawl(self):
    self.mainFrame().load(QUrl(self._url))

def onFinished(self,networkReply):
    if networkReply.rawHeader('Content-Type')=='image/png':
        print 'find the image'
        l=int(networkReply.rawHeader('Content-Length'))
        print l
        byteArray=networkReply.readAll()
        print byteArray.size()
        im=QImage.fromData(byteArray)
        if not im.save('test.jpg','jpg'):
            print 'image save error'


def main():
    app=QApplication(sys.argv)
    url='http://www.yiyaows.cn/DrsPath.do?kid=6666686E686E69673334333632303335&username=mylibddrz&spagenum=251&pages=50&fid=7534992&a=95cb07394dbf1d43c1fe61bdf6d4a36d&btime=2011-08-19&etime=2011-09-08&template=bookdsr1&firstdrs=http%3A%2F%2Fbook1.duxiu.com%2FbookDetail.jsp%3FdxNumber%3D000005609810%26d%3DA30222298F3C6715323B5476CB66D650'
    dx=dxBrowser(url)
    dx.crawl()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()    

Though the content-length is non-zero but the byteArray.size() is 0. So I can't save the image. Why? Can anyone help me.

EDIT:
Maybe I figured this out. The qtwebkit may have retrieve the content of the qnetworkreply, a QIODevice, the size of it would be 0 after readall().Maybe the qtwebkit as a browser has read it for rendering, I guess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-12-07 18:42:43

编辑:也许我明白了。 qtwebkit 可能已经检索到
qnetworkreply 的内容,一个 QIODevice,它的大小为 0
readall()之后。也许qtwebkit作为浏览器已经读取它了
我猜是渲染。

是的,有一个简单的解决方法:将 QNetworkDiskCache 添加到管理器(使用 QNetworkAccessManager.setCache)并检索图像从插槽 onFinished 中的缓存中获取。

如果网站使用“Pragma: no-cache”或“Cache-control”来提示浏览器不要将文件保存到磁盘,您将不得不重新定义方法 prepare (也许 updateMetaData) 的 QNetworkDiskCache 来在调用原始方法之前覆盖 saveToDisk 标志。

EDIT: Maybe I figured this out. The qtwebkit may have retrieve the
content of the qnetworkreply, a QIODevice, the size of it would be 0
after readall().Maybe the qtwebkit as a browser has read it for
rendering, I guess.

Yes and there is an easy work-around: add a QNetworkDiskCache to the manager (with QNetworkAccessManager.setCache) and retrieve the image from the cache in your slot onFinished.

If the website uses "Pragma: no-cache" or "Cache-control" to hint the browser not to save the file to disk, you will have to redefine the method prepare (and maybe updateMetaData) of QNetworkDiskCache to override the flag saveToDisk before calling the original method(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文