为什么 QNetworkReply readAll() 返回零字节?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一个简单的解决方法:将
QNetworkDiskCache
添加到管理器(使用QNetworkAccessManager.setCache
)并检索图像从插槽onFinished
中的缓存中获取。如果网站使用“Pragma: no-cache”或“Cache-control”来提示浏览器不要将文件保存到磁盘,您将不得不重新定义方法
prepare
(也许updateMetaData
) 的 QNetworkDiskCache 来在调用原始方法之前覆盖saveToDisk
标志。Yes and there is an easy work-around: add a
QNetworkDiskCache
to the manager (withQNetworkAccessManager.setCache
) and retrieve the image from the cache in your slotonFinished
.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 maybeupdateMetaData
) ofQNetworkDiskCache
to override the flagsaveToDisk
before calling the original method(s).