【问题标题】:How do I list all photos for my user account?如何列出我的用户帐户的所有照片?
【发布时间】:2014-08-05 06:34:00
【问题描述】:

我正在尝试编写一个执行以下操作的脚本:

  • 从我的 flickr 帐户获取相册(照片集)ID 列表
  • 将每个相册(照片集)中的图片标题列出到名为相册标题的文本文件中

这是我目前所拥有的:

import flickrapi

from xml.etree import ElementTree

api_key = 'xxxx'
api_secret = 'xxxx'

flickr = flickrapi.FlickrAPI(api_key, api_secret)

(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))

sets = flickr.photosets_getList(user_id='xxxx')

for elm in sets.getchildren()[0]:
    title = elm.getchildren()[0].text
    print ("id: %s setname: %s photos: %s") %(elm.get('id'), title, elm.get('photos'))

上面简单地将结果输出到屏幕上,如下所示:

id: 12453463463252553 setname: 2006-08 photos: 371
id: 23523523523532523 setname: 2006-07 photos: 507
id: 53253253253255532 setname: 2006-06 photos: 20
... etc ...

从那里,我得到了以下内容,我认为这些内容会列出上述相册中的所有图片标题:

import flickrapi

from xml.etree import ElementTree

api_key = 'xxxx'
api_secret = 'xxxx'

flickr = flickrapi.FlickrAPI(api_key, api_secret)

(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))

photos = flickr.photosets_getPhotos(photoset_id='12453463463252553')

for elm in photos.getchildren()[0]:
    title = elm.getchildren()[0].text
    print ("%s") %(elm.get('title'))

不幸的是,它只是吐出一个索引超出范围的索引错误。

【问题讨论】:

  • 除了 photoset_id 之外,我发现您的 2 个脚本之间没有区别,为什么它们的输出会不同?
  • 在尝试迭代之前确认 photos.getchildren() 包含数据。 for elm in photos.getchildren()[0] 看起来也是第一个数组元素 - 你确定你不是指 for elm in photos.getchildren():?
  • @Fabricator 啊是的 - 我明白了,我复制了相同的代码块两次。我已经编辑了原始帖子以适应。
  • @LukePeterson 感谢卢克,我从代码中删除了 [0] 并得到了这个:2013-05 我认为它需要在原始 XML 中进入下一个级别。 我想得到标题“2009-12-05 14-25-56”,而不是它上面的那个.再次感谢。
  • @jztilly:将 XML 数据添加到问题中,您可以在其中正确格式化它。同样,如果您遇到异常(例如 IndexError),请包含完整的回溯。

标签: python flickr


【解决方案1】:

如果您使用python-flickr-api,这很容易。复杂的部分是从 flickr 获得授权以访问私人信息。

这是一些您可以使用的(未经测试的)代码:

import os

import flickr_api as flickr

# If all you want to do is get public information,
# then you need to set the api key and secret

flickr.set_keys(api_key='key', api_secret='sekret')

# If you want to fetch private/hidden information
# then in addition to the api key and secret,
# you also need to authorize your application.

# To do that, we request the authorization URL
# to get the value of `oauth_verifier`, which
# is what we need.

# This step is done only once, and we save
# the token. So naturally, we first check
# if the token exists or not:

if os.path.isfile('token.key'):
    flickr.set_auth_handler('token.key')
else:
    # This is the first time we are running,
    # so get the token and save it
    auth = flickr.auth.AuthHandler()
    url = auth.get_authorization_url('read')  # Get read permissions
    session_key = raw_input('''
                 Please visit {} and then copy the value of oauth_verifier:'''.format(url))

    if len(session_key.strip()):
        auth.set_verifier(session_key.strip())
        flickr.set_auth_handler(auth)

        # Save this token for next time
        auth.save('token.key')
    else:
        raise Exception("No authorization token provided, quitting.")

# If we reached this point, we are good to go!
# First thing we want to do is enable the cache, so
# we don't hit the API when not needed

flickr.enable_cache()

# Fetching a user, by their username

user = flickr.Person.findByUserName('username')

# Or, we don't know the username:

user = flickr.Person.findByEmail('some@user.com')

# Or, if we want to use the authenticated user

user = flickr.test.login()

# Next, fetch the photosets and their corresponding photos:

photo_sets = user.getPhotosets()
for pset in photo_sets:
    print("Getting pictures for {}".format(pset.title))
    photos = pset.getPhotos()
    for photo in photos:
        print('{}'.format(photo.info.title))

# Or, just get me _all_ the photos:

photos = user.getPhotos()

# If you haven't logged in,
# photos = user.getPublicPhotos()

for photo in photos:
    print('{}'.format(photo.info.title))

【讨论】:

  • 感谢您,但不幸的是并没有走得太远。我认为自从 flickr 转向仅 HTTPS 连接以来,API 还没有被重写。
  • 其实3个月前是updated for https。
【解决方案2】:

我坚持了下来,并得到了朋友的帮助,想出了以下按计划工作的方法:

import flickrapi
import os
from xml.etree import ElementTree

api_key = 'xxxx'
api_secret = 'xxxx'

flickr = flickrapi.FlickrAPI(api_key, api_secret)

(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))


sets = flickr.photosets_getList(user_id='xxxx')

for set in sets.getchildren()[0]:
    title = set.getchildren()[0].text

    filename = "%s.txt" % (title)
    f = open(filename,'w')

    print ("Getting Photos from set: %s") % (title)

    for photo in flickr.walk_set(set.get('id')):
                f.write("%s" % (photo.get('title')))


    f.close()

【讨论】:

  • 嗯,你如何获得 user_id 呢?等等,我正在寻找它:D
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多