我正在尝试下载带有 Python 3 请求的 zip 文件:
try:
r = requests.get(BASEURL, stream=True)
with open(localfiletitle, 'wb') as fd:
shutil.copyfileobj(r.raw, fd)
except requests.exceptions.RequestException as e:
send_notice_mail("Error downloading the file:", e)
return False
但我总是得到部分无效的文件。我也尝试过使用
urllib.request
,在这种情况下我收到 http.client.IncompleteRead: IncompleteRead
错误。
您需要从
content
提取值,而不是 raw
with open(localfiletitle, 'wb') as fd:
shutil.copyfileobj(io.BytesIO(r.content), fd)
您也可以使用它而不使用 ZipFile
保存它data = io.BytesIO(r.content)
with ZipFile(data) as zip_file:
...