Python解析xml以从数据中检索“title”

时间:2012-11-15 20:51:47

标签: python xml-parsing

我正试图找出一种用python读取URL内容的方法,然后将标题解析为变量。

正在显示/读取的URL数据示例:

<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" debug="true">
    <script id="FirebugLite" firebugIgnore="true" extension="Chrome"/>
    <song id="11450" parent="11042" title="30 Minutes Or Less (2011)" album="30 Minutes Or Less (2011)" isDir="false" created="2011-11-04T16:07:38" duration="4983" bitRate="1536" size="3521040154" suffix="mkv" contentType="video/x-matroska" isVideo="true" path="30 Minutes Or Less (2011)/30 Minutes Or Less (2011).mkv" transcodedSuffix="flv" transcodedContentType="video/x-flv"/>
</subsonic-response>

有没有办法从标题领域获取数据?因此,在一个变量中,我最终会得到'30分钟或更少(2011)'

我现在有了流动的代码,但却遇到了麻烦。

import urllib, urllib2
from xml.etree.ElementTree import ElementTree
url = 'https://myurl.com'
root = urllib.urlopen(url).read()
tester = ElementTree.parse(root)
elem = ElementTree.find("title")
print elem

错误: TypeError:必须使用ElementTree实例作为第一个参数调用unbound方法parse()(改为使用str实例)

0 个答案:

没有答案