2016-04-06 56 views
1
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文件進一步處理它。如何處理來自flickr的xml響應

我想轉換r對象(其是lxml.etree._Element類型) 到xml_input(的lxml.etree._ElementTree類型)。

我用tree = ET.ElementTree(r),但結果是xml.etree.ElementTree.ElementTree

我看到這不完全相同,但我不明白區別。

r如何轉換爲xml_input

回答

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'>