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,830 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
- 181,842
- 47
- 306
- 310
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
- 125,936
- 27
- 200
- 224
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
- 22,259
- 6
- 62
- 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,569
- 6
- 32
- 42
0
import urllib2
open("fish.jpg", "w").write(urllib2.urlopen("http://www.fiskeri.no/Fiskeslag/Fjesing.jpg").read())

Alexander
- 9,737
- 4
- 53
- 59
0
Easy.
import urllib
urllib.urlretrieve("http://www.dokuwiki.org/_media/wiki:dokuwiki-128.png","dafile.png")

Trufa
- 39,971
- 43
- 126
- 190