我想解析下面代碼中嵌入的來自YouTube的xml。我試圖顯示所有的標題。但是,當我嘗試打印「標題」時,我遇到了麻煩,只有輸入的行出現。有什麼建議?如何使用Python解析YouTube XML?
#import library to do http requests:
import urllib2
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
#download the file:
file = urllib2.urlopen('http://gdata.youtube.com/feeds/api/users/buzzfeed/uploads?v=2&max-results=50')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
entry=dom.getElementsByTagName('entry')
for node in entry:
video_title=node.getAttribute('title')
print video_title
請添加要解析的XML摘錄。 – 2012-10-08 14:27:41
請不要使用'minidom'。該文檔告訴您使用['ElementTree' API](http://docs.python.org/library/xml.etree.elementtree.html)。您可以在標準庫中使用包含的版本,或使用擴展該API的優秀外部['lxml'庫](http://lxml.de/)。 –
http://lxml.de/ – sean