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
?
答案 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'>