用于组搜索的python flickr api并获取图像数据集

时间:2015-04-15 13:28:07

标签: python flickr

是否有任何python flickr api,我可以点击组网址并获取所有最新的图像数据?

我有一个网址:

https://www.flickr.com/groups/caterpillarequipment/

我想获取所有最新的图像数据集。 我试着用flickrapi lib做到这一点,但到目前为止能够找出任何方法。 我是flickr的新手,不知道如何获取这些数据。

1 个答案:

答案 0 :(得分:2)

如果您想获取群组池的所有信息,请使用flickr.groups.pools.getPhotos API调用,如下所示:

import flickrapi
from pprint import pprint

api_key = 'your api key'
secret = 'your secret key'
flickr = flickrapi.FlickrAPI(api_key, secret, format='parsed-json')

group_id = '544735@N20'    # caterpillarequipment's group_id
response = flickr.groups.pools.getPhotos(group_id=group_id, per_page=3)
pprint(response)
{u'photos': {u'page': 1,
             u'pages': 1033,
             u'perpage': 3,
             u'photo': [{u'dateadded': u'1428715622',
                         u'farm': 8,
                         u'id': u'16912446348',
                         u'isfamily': 0,
                         u'isfriend': 0,
                         u'ispublic': 1,
                         u'owner': u'71639059@N00',
                         u'ownername': u'ocrr4204',
                         u'secret': u'311b63f966',
                         u'server': u'7685',
                         u'title': u'Dufresne Construction 146 a CAT D7H bulldozer Ottawa, Ontario Canada 04252007 \xa9Ian A. McCord'},
                        {u'dateadded': u'1428713719',
                         u'farm': 8,
                         u'id': u'16896411327',
                         u'isfamily': 0,
                         u'isfriend': 0,
                         u'ispublic': 1,
                         u'owner': u'62532775@N03',
                         u'ownername': u'Jibup',
                         u'secret': u'76dc9110ed',
                         u'server': u'7695',
                         u'title': u'Caterpillar'},
                        {u'dateadded': u'1428554028',
                         u'farm': 8,
                         u'id': u'17058845816',
                         u'isfamily': 0,
                         u'isfriend': 0,
                         u'ispublic': 1,
                         u'owner': u'73369431@N07',
                         u'ownername': u'Stephen Ball Photography',
                         u'secret': u'607f522144',
                         u'server': u'7705',
                         u'title': u'One Up One Down'}],
             u'total': u'3097'},
 u'stat': u'ok'}

我使用了3页的小页面用于演示目的 - 默认值为100,最大值为500.这意味着您需要多次调用该服务才能检索所有照片。你可以像这样循环:

group_pool_photos = []
group_id = '544735@N20'    # caterpillarequipment's group_id
page = 1
success = True
while True:
    response = flickr.groups.pools.getPhotos(group_id=group_id, page=page)
    if response['stat'] != 'ok':
        print 'Error occurred in flickr.groups.pools.getPhotos'
        pprint(response)
        success = False
        break

    if len(response['photos']['photo']) == 0:
        break

    group_pool_photos.extend(response['photos']['photo'])
    page += 1

if success:
    print 'Info for all pool photos successfully retrieved. Total photos {}'.format(len(group_pool_photos))

在此group_pool_photos的末尾应包含所有照片信息。

N.B。由于在检索过程中添加或删除了照片,池可能会发生变化,因此您可能希望通过比较连续响应中的总数来尝试检测它。