【发布时间】:2014-07-05 21:54:46
【问题描述】:
我正在使用requests lib 从站点下载一些图像。
我的代码会在下载后检查文件大小。
示例代码:
def download(url, store_dir):
r = requests.get(url, headers=headers, proxies=proxies)
filename = r.headers.get('content-disposition').split('=')[1]
real_length = int(r.headers.get('content-length'))
wholepath = os.path.join(store_dir, filename)
with open(wholepath, 'wb') as f:
f.write(r.content)
f.close()
if os.path.getsize(wholepath) != real_length:
print('size error')
print('status_code: %s' %r.status_code)
print('headers: %s' %r.headers)
print('url"%s' % url)
print('orgin:', r.headers['content-length'], 'now',os.path.getsize(wholepath))
self.download(url, store_dir)
但我通常发现即使os.path.getsize(wholepath) == real_length 图像文件已损坏。
我该如何解决这个问题?
【问题讨论】:
标签: python python-2.7 download python-requests