파이썬에서 Flickr API를 사용하여 이미지 URL 얻기

하고 싶은 일



Flickr에서 검색 단어와 관련된 이미지를 얻고 싶습니다.

사전 준비


  • Flickr 계정을 만들고 API 키를 가져옵니다.
  • Flickr

  • Flickr에 등록하려면 Yahoo.com 계정이 필요합니다.
  • 야 뺨. 작은 m


  • 개발 환경


  • python 2.7.x 시스템

  • Requirement


    pip install poster 
    

    프로그램



    GitHub에도 올리고 있습니다. → 시모치 굽힘 F 여과 mF ぃckr. py

    getImageFromFlickr.py
    #coding: utf-8
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    import json
    import sys
    import urllib2
    
    
    def getImageUrlFromFlickr(API_KEY, query, N):
    
        NUM_OF_PHOTO = str(N) #取得する画像URLの数
        option = '&sort=relevance&privacy_filter=1&content_type=1&per_page='+ NUM_OF_PHOTO +'&format=json&nojsoncallback=1'
        url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='+ API_KEY + option
    
        #JSON形式で結果を取得
        register_openers()
        datagen, headers = multipart_encode({'text': query})
        request = urllib2.Request(url,datagen, headers)
        response = urllib2.urlopen(request)
        res_dat = response.read()
    
        url_list = [] #URLリスト
        template_url = 'https://farm%s.staticflickr.com/%s/%s_%s.jpg' #URLのテンプレート
        for i in json.loads(res_dat)['photos']['photo']:
            img_url = template_url % (i['farm'],i['server'],i['id'],i['secret'])
            url_list.append(img_url) #リストに画像URLを追加
    
        return url_list
    
    if __name__ == '__main__':
    
        API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
    
        query = sys.argv[1]
        #API KEY, クエリ, 取得した画像数を渡す
        url_list = getImageUrlFromFlickr(API_KEY, query, 10)
    
        #取得した画像URLを表示
        for url in url_list:
            print url
    
    

    실행 결과


    >python getImageFromFlickr.py 寿司
    https://farm6.staticflickr.com/5043/5203889493_b5a2feacf2.jpg
    https://farm5.staticflickr.com/4021/4247841124_fd2f32222e.jpg
    https://farm5.staticflickr.com/4082/4801051243_b439902670.jpg
    https://farm4.staticflickr.com/3384/3472202216_090d54fb52.jpg
    https://farm3.staticflickr.com/2265/2505551269_78f4cb8650.jpg
    https://farm6.staticflickr.com/5296/5479083865_4e6ffa9553.jpg
    https://farm3.staticflickr.com/2609/4008704322_b2c9a9895f.jpg
    https://farm5.staticflickr.com/4117/4801046373_3e492b224d.jpg
    https://farm5.staticflickr.com/4119/4801043517_d3bf9e38b9.jpg
    https://farm5.staticflickr.com/4087/5025254420_131c78cb35.jpg
    

    획득한 이미지



    htps // // rm6. s들 cfぃckr. m / 5043 / 5203889493_b5 A2 FU Cf2. jpg

    좋은 웹페이지 즐겨찾기