当我使用python通过urllib2 request或urllib.urlretrieve从url保存图像时,我遇到了问题.这是图像的网址是有效的.我可以使用资源管理器手动下载它.但是,当我使用python下载图像时,无法打开该文件.我使用Mac OS预览来查看图像.谢谢!
更新:
代码如下
def downloadImage(self):
request = urllib2.Request(self.url)
pic = urllib2.urlopen(request)
print "downloading: " + self.url
print self.fileName
filePath = localSaveRoot + self.catalog + self.fileName + Picture.postfix
# urllib.urlretrieve(self.url, filePath)
with open(filePath, 'wb') as localFile:
localFile.write(pic.read())
Run Code Online (Sandbox Code Playgroud)
我想下载的图片网址是 http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg
此URL有效,我可以通过浏览器保存,但python代码将下载无法打开的文件.预览说"它可能已损坏或使用预览无法识别的文件格式".我比较了我用Python下载的图像和我通过浏览器手动下载的图像.前者的大小要小几个字节.所以似乎文件未完成,但我不知道为什么python无法完全下载它.