I want to download some images from flickr. For example, I have this link http://www.flickr.com/photos/tonynetone/3717759677/ and I want to save in my disk this image of size large with a specific name. The documentation in this link did not helped me a lot. Could you please some one give an example of python code?
Thank you.
PS: windows
Do you need a pure Python solution? How do you like a Bash one-liner like this?
$ curl http://www.flickr.com/photos/tonynetone/3717759677/ | egrep 'http:\\/\\/[^"]*jpg' -o | sed -e 's@\\@@g'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 177k 0 177k 0 0 164k 0 --:--:-- 0:00:01 --:--:-- 309k
http://farm3.staticflickr.com/2567/3717759677_4a520a1dbb_s.jpg
http://farm3.staticflickr.com/2567/3717759677_4a520a1dbb_q.jpg
http://farm3.staticflickr.com/2567/3717759677_4a520a1dbb_t.jpg
http://farm3.staticflickr.com/2567/3717759677_4a520a1dbb_m.jpg
http://farm3.staticflickr.com/2567/3717759677_4a520a1dbb.jpg
http://farm3.staticflickr.com/2567/3717759677_97b2ab3b4d_o.jpg
By the way there's no "large" option for the photo you referred to
Perhaps you meant "original" (_o
) ?
import urllib2, re
def download(url, save_name):
html = urllib2.urlopen(url).read()
img_url = re.findall(r'http:[^" \\:]*_b\.jpg', html)[0]
with open(save_name, "wb") as fp:
fp.write(urllib2.urlopen(img_url).read())
download('http://www.flickr.com/photos/91440301@N03/9679566882/sizes/l/', 'elephant.jpg')
Usage notes
If there is no "large" option (a photo whose filename ends with _b.jpg
), it will just raise an exception (perhaps IndexError
.) Please catch it by yourself.
Supports only .jpg
Please determine save_name
by yourself.