문제

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