flickAPI:如何列出我的用户帐户的集合?
我希望列出该用户创建的所有集以及集 ID。我正在使用 flickrApi 和 Python。这是我的代码。 setName 在输出中返回“none”。 elm.get('title') 返回“none”。
import flickrapi
api_key = 'APIKEY'
api_secret = 'APISECRET'
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))
try:
favs = flickr.photosets_getList(user_id='51784048@N00')
#favs = flickr.favorites_getPublicList(user_id = '51784048@N00')
for elm in favs.getiterator():
print ("id: %s secret: %s setname: %s") %(elm.get('id'), elm.get('secret'), elm.get('title'))
except:
raise Exception("Some error encountered!")
I wish to list all the sets created by this user along with the set id. I am using flickrApi and Python. Here's my code. The setName is returning "none" in the output.
The elm.get('title') is returning "none".
import flickrapi
api_key = 'APIKEY'
api_secret = 'APISECRET'
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))
try:
favs = flickr.photosets_getList(user_id='51784048@N00')
#favs = flickr.favorites_getPublicList(user_id = '51784048@N00')
for elm in favs.getiterator():
print ("id: %s secret: %s setname: %s") %(elm.get('id'), elm.get('secret'), elm.get('title'))
except:
raise Exception("Some error encountered!")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 Python 中使用
flickrapi
时,打印 API 调用返回的 XML 通常很有用,以便查看发生了什么。 添加:在您的示例中,如果您在顶部
...,然后添加: ... 您将看到返回的结构是:
在真实的应用程序中,您需要检查返回状态并检查 < code>page 属性,但为了快速获得您想要的内容,我们只看一下
元素。要迭代它们,您可以将循环更改为:然后您将看到必须导航的每个
photoset
元素的结构。例如,一个是:... 因此标题实际上存储在
title
子元素中。由此,您可以看到,要获取所需的信息,您可以执行以下操作:... 生成输出:
等等。
When you're working with
flickrapi
in Python, it's frequently useful to print the XML which is returned by the API calls in order to see what's going on. In your example, if you add:... at the top, and then add:
... you'll see that the structure returned is:
In a real application, you'd want to check the return status and check the
page
attributes, but to quickly get to what you want, let's just look at the<photoset>
elements. To iterate over them, you can change your loop to:Then you'll see the structure of each
photoset
element that you'll have to navigate. For example, one would be:... so the title is actually stored in a
title
sub-element. From that, you can see that to get the information that you want, you can do:... which produces the output:
etc.