I have this code:
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()
When trying the code I receive the following error:
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
I have been trying to combine solutions from here and here with no luck. Can anyone help me? Thanks
I have used this solution for my project when I needed to download .gz files, maybe it will work for you.
from urllib.request import Request as urllibRequest
request = urllibRequest(url)
with open(file_name, 'wb') as output:
output.write(urlopen(request).read())
I think the remote server can cause this issue. The server expects user headers and if there are no headers, then cause a redirect. As you mentioned, it is described here
Try to add user headers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.