2014-10-08 90 views
0
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文件。你怎麼蟒蛇flickrapi響應對象保存爲XML文件

我正在使用flickr.photos.getInfo方法來收集有關圖像的數據。對不起,但我需要的細節來克服錯誤<Element rsp at 101463b00>。順便說一句,我從一張圖片開始,看看代碼是如何工作的。

回答

0

也許這是有幫助的

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

,這可能由亞歷克西斯·米尼翁也很感興趣

Python Flickr API

+0

其實我試過的代碼,但我有一個錯誤「typeError:<元素rsp在101574f80>不是JSON序列化」。我有這個obj「data = flickrClient.photos_getinfo(photo_id ='photoid')」,我想將響應屬性寫入一個可以使用數據的文件中。 – Samah 2014-10-08 04:03:41

+0

對不起,[this](https://www.flickr.com/services/api/flickr.photos.getInfo.html)實際上給了XML作爲迴應。你能發佈你的代碼嗎? [Here](http://stackoverflow.com/questions/4451600/python-newbie-parse-xml-from-api-call)是一個類似的問題。 tree.write('output.xml')寫入文件。 – toas939 2014-10-08 04:31:59

0
import flickrapi 
import os 
from xml.etree import ElementTree 

api_key = '6613deslkjfsldfsödflsdjfsjdla7e' 
api_secret = '35lksdjöfadfs975' 
user_id = '[email protected]' 


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

+0

我將Flickr響應存儲爲xml文件,但我想要保存的是txt文件中的屬性,如:image id = 1234 userId = ... tilte = ...等等。我使用了.getroot(),我可以存儲第一個元素的屬性,但是我無法得到其餘的內容。我會在下面說明我的代碼。 – Samah 2014-10-12 22:42:05

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以獲取其信息。在那裏我可以將它用作需要這些數據操作的算法的輸入。