سؤال

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

هل كانت مفيدة؟

المحلول 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)

نصائح أخرى

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top