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