你也可以使用 urllib2 吗?如果是,您可以使用此功能:
def download_file(url):
"""Create an urllib2 request and return the request plus some useful info"""
name = filename_from_url(url)
r = urllib2.urlopen(urllib2.Request(url))
info = r.info()
if 'Content-Disposition' in info:
# If the response has Content-Disposition, we take filename from it
name = info['Content-Disposition'].split('filename=')[1]
if name[0] == '"' or name[0] == "'":
name = name[1:-1]
elif r.geturl() != url:
# if we were redirected, take the filename from the final url
name = filename_from_url(r.geturl())
content_type = None
if 'Content-Type' in info:
content_type = info['Content-Type'].split(';')[0]
# Try to guess missing info
if not name and not content_type:
name = 'unknown'
elif not name:
name = 'unknown' + mimetypes.guess_extension(content_type) or ''
elif not content_type:
content_type = mimetypes.guess_type(name)[0]
return r, name, content_type
用法:
fp, filename, content_type = download_file('http://url/to/some/file')
with open('somefile', 'w') as dst:
shutil.copyfileobj(fp, dst)
此代码的优点是永远不会将整个文件读入内存 - 因此它也适用于大文件。除此之外,它还为您提供从服务器接收的文件名和内容类型,以防您需要/需要它。