Frage

I can download a file from URL the following way.

import urllib2
response = urllib2.urlopen("http://www.someurl.com/file.pdf")
html = response.read()

One way I can think of is open this file as binary and then resave it to the differnet folder I want to save

but is there a better way?

Thanks

War es hilfreich?

Lösung 2

The function you are looking for is urllib.urlretrieve

import urllib
linkToFile = "http://www.someurl.com/file.pdf"
localDestination = "/home/user/local/path/to/file.pdf"
resultFilePath, responseHeaders = urllib.urlretrieve(linkToFile, localDestination)

Andere Tipps

You can use the python module wget for downloading the file. Here is a sample code

import wget
url = 'http://www.example.com/foo.zip'
path = 'path/to/destination'
wget.download(url,out = path)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top