How can I grab a picture off of a known url and save it to my computer using Python (v2.6)? Thanks
Asked
Active
Viewed 4,748 times
6 Answers
8
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'
miku
- 172,072
- 46
- 300
- 307
2
You can use urllib.urlretrieve:
import urllib
urllib.urlretrieve('http://example.com/file.png', './file.png')
If you need more flexibility, use urllib2.
Cat Plus Plus
- 119,938
- 26
- 191
- 218
1
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')"
Rob Cowie
- 21,692
- 6
- 61
- 56
-
How does one make an authenticated request? And does it make a difference as to which one you go for? – user3624582 Sep 07 '14 at 10:22
0
batteries are included in urllib:
urllib.urlretrieve(yourUrl, fileName)
Karmic Coder
- 17,132
- 5
- 32
- 41
0
import urllib2
open("fish.jpg", "w").write(urllib2.urlopen("http://www.fiskeri.no/Fiskeslag/Fjesing.jpg").read())
Alexander
- 8,999
- 3
- 49
- 58
0
Easy.
import urllib
urllib.urlretrieve("http://www.dokuwiki.org/_media/wiki:dokuwiki-128.png","dafile.png")
Trufa
- 38,078
- 41
- 121
- 186