2010-07-05 24 views

回答

4

的结果是正确的 - 按照你给的网址,该XML节点是空的(加/减换行符和空格字符,显然)。 favs.text会返回内容,但您要查找的是属性。试试这个:

for photo in favs.find('photos').findall('photo'): 
    print photo.get('id') 

结果:

'445267544' 
'3334987037' 

或所有子节点,从根开始:

for elm in favs.getiterator(): 
    print elm.items() 

结果:

[('stat', 'ok')] 
[('total', '2'), ('perpage', '100'), ('page', '1'), ('pages', '1')] 
[('isfamily', '0'), ('title', 'The Giants of Africa'), ('farm', '1'), ('ispublic', '1'), ('server', '218'), ('isfriend', '0'), ('secret', '992df924aa'), ('owner', '[email protected]'), ('id', '445267544'), ('date_faved', '1273873654')] 
[('isfamily', '0'), ('title', 'Lava Light - Maui, Hawaii'), ('farm', '4'), ('ispublic', '1'), ('server', '3401'), ('isfriend', '0'), ('secret', '2fa1856916'), ('owner', '[email protected]'), ('id', '3334987037'), ('date_faved', '1273873515')] 
+0

的第二行代码应该是:print photo.get('id') – compie 2010-07-18 15:09:45

+0

谢谢,更正了。我当然在交互式控制台中运行它。 – chryss 2010-07-19 07:32:58