2010-07-05 51 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