我想从网站下载动态生成的图像。该网站有 javascript 代码和单击按钮转到上一个图像和下一个图像。我检查了 chrome 中的 http 请求和响应。除了图像名称外,请求几乎相同(数字增加,如:)000001.jpg,000002.jpg
。现在我可以访问第一个图像并将其保存到磁盘,方法是使用自定义的 QNetworkAccessManager 子类化 QWebView。我重载了 createRequest 函数:
import sys,urllib,time,os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from PIL import Image
class NetworkAccessManager(QNetworkAccessManager):
def __init__(self,old_manager):
QNetworkAccessManager.__init__(self)
self.old_manager = old_manager
self.setCache(old_manager.cache())
self.setCookieJar(old_manager.cookieJar())
self.setProxy(old_manager.proxy())
self.setProxyFactory(old_manager.proxyFactory())
self.imreply=None
self.reqstr=None
self.otherreply=None
self.current_req=None
self.cnt=0
self.jpgName="test.jpg"
self.first=True
self.ba=QByteArray()
self.ba.clear()
def createRequest(self, operation, request, data):
req = request.url().toString()
if req.contains(QString("zoom=")) and req.contains(QString("ss2jpg")) and not req.contains(QString("pi=2")):
strreq=str(req)
l=strreq.find("jid=")
r=strreq.find(".jpg&a")
self.jpgName=strreq[l+5:r+4]
self.jpgcnt=int(strreq[l+5:r])
print self.jpgName,self.jpgcnt
self.imreply=QNetworkAccessManager.createRequest(self,operation, request, data)
self.connect(self.imreply,SIGNAL("readyRead()"),self.saveImage)
return self.imreply
elif req.contains(QString("uf=ssr")):
strreq=str(req)
self.reqstr=strreq
self.current_req=request
r=strreq.find("?")
self.jpgcnt=int(strreq[r-6:r])
self.otherreply=QNetworkAccessManager.createRequest(self,operation, request, data)
return self.otherreply
else:
return QNetworkAccessManager.createRequest(self,operation, request, data)
def saveImage(self):
if self.imreply.header(QNetworkRequest.ContentTypeHeader).toString().contains(QString("image/jpeg")) or self.imreply.header(QNetworkRequest.ContentTypeHeader).toString().contains(QString("image/png")):
contentLen,flag = QString(self.imreply.rawHeader("Content-Length")).toInt()
self.ba=self.ba.append(self.imreply.readAll())
if self.ba.size() == contentLen:
#self.ba=self.imreply.readAll()
im=QImage.fromData(self.ba)
im.save(self.jpgName)
im=Image.open(self.jpgName)
print "saving image",contentLen,self.jpgName
im.save(self.jpgName)
self.ba.clear()
self.emit(SIGNAL("nextPage()"))
class dxWebView(QWebView):
def __init__(self):
QWebView.__init__(self)
def clickNext(self):
manager=self.page().networkAccessManager()
if manager.cnt<50:
nextreq=manager.current_req
nexturl=manager.reqstr.replace(str(manager.jpgcnt),str(manager.jpgcnt+1))
print "next url",nexturl
nextreq.setUrl(QUrl(nexturl))
manager.get(QNetworkRequest(nextreq))
manager.cnt=manager.cnt+1
def main():
app=QApplication(sys.argv)
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True);
view=dxWebView()
old_manager=view.page().networkAccessManager()
new_manager=NetworkAccessManager(old_manager)
view.page().setNetworkAccessManager(new_manager)
QObject.connect(new_manager,SIGNAL("nextPage()"),view.clickNext)
url="http://www.yishuleia.cn/DrsPath.do?kid=686A67696A6F6A673134343438303337&username=gdnz2&spagenum=201&pages=50&fid=14813857&a=3fc3e380601ced0f08749c964294120e&btime=2013-04-03&etime=2013-04-23&template=bookdsr1&firstdrs=http%3A%2F%2Fbook.duxiu.com%2FbookDetail.jsp%3FdxNumber%3D000008299393%26d%3D592DC22226A893A958A6578E7D039A43"
view.load(QUrl(url))
view.show()
sys.exit(app.exec_())
if __name__=='__main__':
main()
保存第一张图片后,触发clickNext,qnetworkaccessmanager发送下一个请求。但我发现manager.get(nextreq)不起作用。http分析器没有筛选任何http请求和响应。我在 clickNext 功能中错了吗?这个怎么做?谢谢!