Python 如何解析特定条目的youtube xml?

Python 如何解析特定条目的youtube xml?,python,xml,parsing,youtube,Python,Xml,Parsing,Youtube,我试图返回视频的持续时间,但遇到了问题 #YOUTUBE FEED #download the file: file = urllib2.urlopen('http://gdata.youtube.com/feeds/api/videos/2s0vk2wEMtA') #convert to string: data = file.read() #close file because we dont need it anymore: file.close() #entire feed root

我试图返回视频的持续时间,但遇到了问题

#YOUTUBE FEED
#download the file:
file = urllib2.urlopen('http://gdata.youtube.com/feeds/api/videos/2s0vk2wEMtA')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()

#entire feed
root = etree.fromstring(data)

for entry in root:
    for item in entry:
        print item
打印项目时,我将其视为最后一个元素:

Element '{http://gdata.youtube.com/schemas/2007}duration' at 0x10c4fb7d0
但我不知道如何从中获得价值。有什么建议吗?

media=root.find('{http://search.yahoo.com/mrss/}组') 长度=媒体。查找('{http://gdata.youtube.com/schemas/2007}duration').attrib.get(“秒”)
打印长度

注意,使用json返回数据更简单、更清晰。例如,看看这个url,您将看到提取持续时间是多么容易。我知道大块代码在注释中看起来很难看,但您可以使用:
import json import urllib2 urlfile=urllib2.urlopen('http://gdata.youtube.com/feeds/api/videos/2s0vk2wEMtA?v=2&alt=jsonc')jsonString=urlfile.read()urlfile.close()jsonData=json.loads(jsonString)打印jsonData[“数据”][“持续时间”]