我有此代码:
import shutil
import urllib.request
import zipfile
url = "http://wwww.some-file.com/my-file.zip"
file_name = url.split('/')[-1]
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
with zipfile.ZipFile(file_name) as zf:
zf.extractall()
尝试代码时,出现以下错误:
urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
答案 0 :(得分:0)
当我需要下载.gz文件时,我已经在项目中使用了此解决方案,也许它会为您工作。
from urllib.request import Request as urllibRequest
request = urllibRequest(url)
with open(file_name, 'wb') as output:
output.write(urlopen(request).read())
答案 1 :(得分:0)
我认为远程服务器可能会导致此问题。服务器需要用户标头,如果没有标头,则导致重定向。如您所述,它是here的描述
尝试添加用户标头