如何处理来自flickr的xml响应

时间:2016-04-06 14:50:26

标签: python xml xslt lxml flickr

import flickrapi
from xml.etree import ElementTree as ET
from lxml import etree

flickr = flickrapi.FlickrAPI(api_key,secret=api_secret)
r = flickr.photos_search(tags='e-waste', has_geo="1", per_page='100')
tree = ET.ElementTree(r) 

xml_input = etree.parse("response_clean.xml")  
transform = etree.XSLT(xslt_root)
links = str(transform(xml_input))

这个小脚本的想法是从Flickr获取xml响应,然后使用xsl文件进一步处理它。

我想转换r对象(类型为lxml.etree._Element) 至xml_input(类型为lxml.etree._ElementTree)。

我使用了tree = ET.ElementTree(r),但结果类型为xml.etree.ElementTree.ElementTree

我看到这不完全相同,但我不明白其中的区别。

r应如何转换为xml_input

1 个答案:

答案 0 :(得分:0)

代码会创建xml.etree.ElementTree.ElementTree,因为相应的ET语句中的import会引用xml.etree.ElementTree。您应该使用etree.ElementTree来代替lxml

>>> from xml.etree import ElementTree as ET
>>> from lxml import etree
>>> raw ='''<root></root>'''
>>> r = etree.fromstring(raw)
>>> root = etree.ElementTree(r)
>>> type(r)
<type 'lxml.etree._Element'>
>>> type(root)
<type 'lxml.etree._ElementTree'>