我想下载这个文件,它可能是zip/7z。当我使用下面的代码时,它为7z文件提供了一个错误。
import requests, zipfile, StringIO
zip_file_url = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
try:
r = requests.get(zip_file_url, stream=True)
z = zipfile.ZipFile(StringIO.StringIO(r.content))
except requests.exceptions.ConnectionError:
print "Connection refused"发布于 2018-07-05 12:50:24
在请求文件时,只需确保HTTP状态代码为200,并以二进制模式写出文件:
import os
import requests
URL = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
filename = os.path.basename(URL)
response = requests.get(URL, stream=True)
if response.status_code == 200:
with open(filename, 'wb') as out:
out.write(response.content)
else:
print('Request failed: %d' % response.status_code)如果请求成功,则下载的文件将出现在脚本正在运行的目录中,或者指示无法下载该文件。
https://stackoverflow.com/questions/51191625
复制相似问题