Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python download without supplying a filename

How do I download a file with progress report using python but without supplying a filename.

I have tried urllib.urlretrieve but I seem to have to supply a filename for the downloaded file to save as.

So for example:

I don't want to supply this:

urllib.urlretrieve("http://www.mozilla.com/products/download.html?product=firefox-3.6.3&os=win&lang=en-US", "/tmp/firefox.exe")

just this:

urllib.urlretrieve("http://www.mozilla.com/products/download.html?product=firefox-3.6.3&os=win&lang=en-US", "/tmp/")

but if I do I get this error:

IOError: [Errno 21] Is a directory: '/tmp'

Also unable to get the filename from some URL Example:

http://www.mozilla.com/products/download.html?product=firefox-3.6.3&os=win&lang=en-US

like image 871
Samuel Taylor Avatar asked May 08 '10 19:05

Samuel Taylor


People also ask

How do we download a file and save it to hard drive using Request module?

You can download files from a URL using the requests module. Simply, get the URL using the get method of requests module and store the result into a variable “myfile” variable. Then you write the contents of the variable into a file.


1 Answers

Here is a complete way to do it with python3 and no filename specified in url:

from urllib.request import urlopen
from urllib.request import urlretrieve
import cgi

url = "http://cloud.ine.ru/s/JDbPr6W4QXnXKgo/download"
remotefile = urlopen(url)
blah = remotefile.info()['Content-Disposition']
value, params = cgi.parse_header(blah)
filename = params["filename"]
urlretrieve(url, filename)

In result you should get cargo_live_animals_parrot.jpg file

like image 135
Ivan Talalaev Avatar answered Nov 09 '22 21:11

Ivan Talalaev