gpt4 book ai didi

python - 如何列出我的用户帐户的所有照片?

转载 作者:行者123 更新时间:2023-11-28 18:42:43 27 4
gpt4 key购买 nike

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

  • 从我的 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'))

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

最佳答案

我坚持了下来,并在 friend 的帮助下提出了以下按计划工作的方法:

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()

关于python - 如何列出我的用户帐户的所有照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com