flickAPI:如何列出我的用户帐户的集合?

发布于 2024-12-02 19:43:20 字数 751 浏览 1 评论 0原文

我希望列出该用户创建的所有集以及集 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-12-09 19:43:20

当您在 Python 中使用 flickrapi 时,打印 API 调用返回的 XML 通常很有用,以便查看发生了什么。 添加:

 from xml.etree import ElementTree

在您的示例中,如果您在顶部

print ElementTree.tostring(favs)

...,然后添加: ... 您将看到返回的结构是:

<rsp stat="ok">
<photosets page="1" pages="1" perpage="80" total="80">
    <photoset .../>
    <photoset .../>
    <photoset .../>
    <photoset ...>
</photosets>
</rsp>

在真实的应用程序中,您需要检查返回状态并检查 < code>page 属性,但为了快速获得您想要的内容,我们只看一下 元素。要迭代它们,您可以将循环更改为:

for elm in favs.getchildren()[0]:
    print ElementTree.tostring(elm)

然后您将看到必须导航的每个 photoset 元素的结构。例如,一个是:

<photoset can_comment="1"
          count_comments="0"
          count_views="34"
          date_create="1156703089"
          date_update="1297462539"
          farm="1"
          id="72157594253605858"
          needs_interstitial="0"
          photos="73" primary="226222345"
          secret="63fde66413"
          server="62"
          videos="0"
          visibility_can_see_set="1">
    <title>Birds</title>
    <description />
</photoset>

... 因此标题实际上存储在 title 子元素中。由此,您可以看到,要获取所需的信息,您可以执行以下操作:

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

... 生成输出:

id: 72157600139832705 secret: 4e884f3523 setname: French Creek State Park
id: 72157600047937451 secret: d3c84ed8df setname: Las Vegas
id: 72157594253605858 secret: 63fde66413 setname: Birds

等等。

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:

 from xml.etree import ElementTree

... at the top, and then add:

print ElementTree.tostring(favs)

... you'll see that the structure returned is:

<rsp stat="ok">
<photosets page="1" pages="1" perpage="80" total="80">
    <photoset .../>
    <photoset .../>
    <photoset .../>
    <photoset ...>
</photosets>
</rsp>

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:

for elm in favs.getchildren()[0]:
    print ElementTree.tostring(elm)

Then you'll see the structure of each photoset element that you'll have to navigate. For example, one would be:

<photoset can_comment="1"
          count_comments="0"
          count_views="34"
          date_create="1156703089"
          date_update="1297462539"
          farm="1"
          id="72157594253605858"
          needs_interstitial="0"
          photos="73" primary="226222345"
          secret="63fde66413"
          server="62"
          videos="0"
          visibility_can_see_set="1">
    <title>Birds</title>
    <description />
</photoset>

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

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

... which produces the output:

id: 72157600139832705 secret: 4e884f3523 setname: French Creek State Park
id: 72157600047937451 secret: d3c84ed8df setname: Las Vegas
id: 72157594253605858 secret: 63fde66413 setname: Birds

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文