Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将python flickrapi响应对象保存为XML文件_Python_Api_Flickr - Fatal编程技术网

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

如何将python flickrapi响应对象保存为XML文件,python,api,flickr,Python,Api,Flickr,我试图使用ElementTree将Flickr响应写入文件以保存信息, 但我的尝试没有成功。如何将obj值保存为xml/txt文件 我正在使用flickr.photos.getInfo方法收集有关图像的数据。很抱歉,我需要详细信息来克服错误。顺便说一句,我从一张图片开始,看看代码是如何工作的。也许这会有帮助 import sys import os import urllib from xml.etree.ElementTree import ElementTree import fli

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

我正在使用
flickr.photos.getInfo
方法收集有关图像的数据。很抱歉,我需要详细信息来克服错误
。顺便说一句,我从一张图片开始,看看代码是如何工作的。

也许这会有帮助

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)
这可能也很有趣

亚历克西斯·米格农的

import json
with open('data.txt', 'w') as outfile:
  json.dump(data, outfile)
查找您的Flickr ID

导入系统
导入操作系统
导入URL库
从xml.etree.ElementTree导入ElementTree
从xml.etree.ElementTree导入到字符串
导入flickrapi
api_键=“”
api_密码=“”
照片_id='2124494179'
flickr=flickrapi.flickrapi(api_密钥,api_密码)
#photos=flickr.photos\u getinfo(photo\u id='15295705890')
#tree=ElementTree(flickr.photos\u getinfo(photo\u id))
#image_id=open('photoIds.txt','r')
#Image\u data=open('imageinformation','w')
#e=图像id.读线(10)
#f=[s.replace('\r\n','')代表e中的s]
#行数=len(f)
#图像_id.close()
#i=0

#虽然我实际上尝试了代码,但我有一个错误“typeError:IsNotJSON serializable”。我有一个obj“data=flickrClient.photos\u getinfo(photo\u id='photoid')”,我想把响应属性写在一个文件中,在这个文件中我可以使用数据。对不起,实际上是将XML作为响应。你能发布你的代码吗?这是一个类似的问题。tree.write('output.xml')写入文件。我将Flickr响应存储为xml文件,但我要保存的是txt文件中的属性,如:image id=1234 userId=。。。倾斜=。。。等等我使用了.getroot(),在这里我可以存储第一个元素的属性,但无法获取其余的属性。我将在下面说明我的代码。
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()
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']