Reputation: 5979
How can I grab a picture off of a known url and save it to my computer using Python (v2.6)? Thanks
Upvotes: 2
Views: 4886
Reputation: 22619
In the absence of any context, the following is a simple example of using standard library modules to make an non-authenticated HTTP GET request
import urllib2
response = urllib2.urlopen('http://lolcat.com/images/lolcats/1674.jpg')
with open('lolcat.jpg', 'wb') as outfile:
outfile.write(response.read())
EDIT: urlretrieve()
is new to me. I guess then you could turn it into a command line one-liner... if you're bored.
$ python -c "import urllib; urllib.urlretrieve('http://lolcat.com/images/lolcats/1674.jpg', filename='/tmp/1674.jpg')"
Upvotes: 1
Reputation: 40737
Easy.
import urllib
urllib.urlretrieve("http://www.dokuwiki.org/_media/wiki:dokuwiki-128.png","dafile.png")
Upvotes: 0
Reputation: 10396
import urllib2
open("fish.jpg", "w").write(urllib2.urlopen("http://www.fiskeri.no/Fiskeslag/Fjesing.jpg").read())
Upvotes: 0
Reputation: 129944
You can use urllib.urlretrieve
:
import urllib
urllib.urlretrieve('http://example.com/file.png', './file.png')
If you need more flexibility, use urllib2
.
Upvotes: 2
Reputation: 17959
batteries are included in urllib:
urllib.urlretrieve(yourUrl, fileName)
Upvotes: 0
Reputation: 188164
You can use urllib.urlretrieve
.
Copy a network object denoted by a URL to a local file, if necessary.
Example:
>>> import urllib
>>> urllib.urlretrieve('http://i.imgur.com/Ph4Xw.jpg', 'duck.jpg')
('duck.jpg', <httplib.HTTPMessage instance at 0x10118e830>)
# by now the file should be downloaded to 'duck.jpg'
Upvotes: 8