J'essaye d'analyser le xml de YouTube qui est incorporé dans le code ci-dessous. J'essaie d'afficher tous les titres. Cependant, je rencontre des problèmes lorsque j'essaie d'imprimer le «titre», seules les lignes apparaissent. Aucun conseil?Comment analyser YouTube XML en utilisant Python?
#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
Veuillez ajouter un extrait du XML que vous voulez analyser. –
Veuillez ne pas utiliser 'minidom'. La documentation vous dit d'utiliser l'API ['ElementTree'] (http://docs.python.org/library/xml.etree.elementtree.html) à la place. Vous pouvez utiliser la version incluse dans la bibliothèque standard ou utiliser l'excellente bibliothèque ['lxml' external] (http://lxml.de/) qui se développe sur cette API. –
http://lxml.de/ – sean