문제

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) ?

Update

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

  1. 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.

  2. Supports only .jpg

  3. Please determine save_name by yourself.

다른 팁

I usually download an image as below

import urllib2

def get_image(url, image_save_name):
    try:
        image = urllib2.urlopen(url).read()
        with open(image_save_name + '.' + url.split('.')[-1], 'wb') as image_file:
            image_file.write(image)
            image_file.close()
    except Exception as e:
        print e

get_image('http://farm3.staticflickr.com/2277/2046712361_77f514172f_z.jpg', 'image_save_name')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top