如何将python flickrapi响应对象保存为XML文件

时间:2014-10-08 03:36:27

标签: python api flickr

import sys

import os

import urllib

from xml.etree.ElementTree import ElementTree

import flickrapi

api_key = 'myApiKey'

api_password = 'Mysecret'

flickrClient = flickrapi.FlickrAPI(api_key, api_password)

photos = flickrClient.photos_getinfo(photo_id='2124494179')

//tree=ElementTree(flickrClient.photos_getinfo)

print (photos)

我试图将Flickr响应写入文件以使用ElementTree保存信息, 但我的尝试没有成功。如何将obj值保存为xml/txt文件。

我正在使用flickr.photos.getInfo方法收集有关图片的数据。抱歉,我需要详细信息才能克服错误<Element rsp at 101463b00>。顺便说一下,我从一张图片开始,看看代码是如何工作的。

3 个答案:

答案 0 :(得分:0)

也许这很有用

import json
with open('data.txt', 'w') as outfile:
  json.dump(data, outfile)

这也很有趣

Alexis Mignon

Python Flickr API

答案 1 :(得分:0)

import flickrapi
import os
from xml.etree import ElementTree

api_key = '6613deslkjfsldfsödflsdjfsjdla7e'
api_secret = '35lksdjöfadfs975'
user_id = '34543535@XXX'


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)

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

找出你的Flickr-ID idGettr

答案 2 :(得分:0)

import sys
import os
import urllib
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import tostring
import flickrapi

 api_key = '             '
 api_password = '     '
 photo_id='2124494179'
 flickr= flickrapi.FlickrAPI(api_key, api_password)
#photos= flickr.photos_getinfo(photo_id='15295705890')
#tree=ElementTree(flickr.photos_getinfo(photo_id))
#image_id=open('photoIds.txt','r')
#Image_data=open('imageinformation','w')
#e=image_id.readlines(10)
#f= [s.replace('\r\n', '') for s in e]
#num_of_lines=len(f)
#image_id.close()
#i=0
#while i<269846:
# term=f[i]
#try:
photoinfo=flickr.photos_getinfo(photo_id=photo_id)
photo_tree=ElementTree(photoinfo)
#photo_tree.write('photo_tree')
#i+=1
#photo=photo_tree.getroot()
#photodata=photo.getiterator()
#for elem in owner.getiterator():
#for elem in photo.getiterator():
for elem in photo_tree.getroot():
  farm=elem.attrib['farm']
  id=elem.attrib['id']
  server=elem.attrib['server']
  #title=photo_tree.find('title').txt
   #for child in elem.findall():
     # username=child.attrib['username']
     # location=child.attrib['location']
     #  user=elem.attrib['username']
print (farm)
print(id)
print(server)
strong text#owner=photo_tree.findall('owner')
# print(username)
#filename="%s.txt"%(farm)
#f=open(filename,'w')
#f.write("%s"%farm)
#for elem in photo_tree.getiterator():
 #for child in photo_tree.getiterator():
 #print (child.attrib)
 #owner=child.attrib['username']

我需要将属性存储在txt文件中,因为我必须从文件中读取大量的图像ID以获取其信息。我可以将它用作需要这些数据运算的算法的输入。