Python 用ElementTree解析XML

Python 用ElementTree解析XML,python,xml,parsing,lxml,elementtree,Python,Xml,Parsing,Lxml,Elementtree,我试图使用ElementTree在XML字符串中搜索标记和属性。以下是字符串: '<?xml version="1.0" encoding="UTF-8" ?>\n<uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.01">\n\t<status success="true" statusCode="2000"/>\n\t<readCalls>\n\t<

我试图使用ElementTree在XML字符串中搜索标记和属性。以下是字符串:

'<?xml version="1.0" encoding="UTF-8" ?>\n<uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.01">\n\t<status success="true" statusCode="2000"/>\n\t<readCalls>\n\t<classify id="thing">\n\t\t<classification textCoverage="0">\n\t\t\t<class className="Astronomy" p="0.333333"/>\n\t\t\t<class className="Biology" p="0.333333"/>\n\t\t\t<class className="Mathematics" p="0.333333"/>\n\t\t</classification>\n\t</classify>\n\t</readCalls>\n</uclassify>'
我认为使用
tree.find('uclassify')
将返回该元素/标记,但它似乎什么也不返回。我还尝试:

for i in tree.iter():
    print i
打印一些东西,但不是我想要的:

<Element '{http://api.uclassify.com/1/ResponseSchema}uclassify' at 0x1011ec410>
<Element '{http://api.uclassify.com/1/ResponseSchema}status' at 0x1011ec390>
<Element '{http://api.uclassify.com/1/ResponseSchema}readCalls' at 0x1011ec450>
<Element '{http://api.uclassify.com/1/ResponseSchema}classify' at 0x1011ec490>
<Element '{http://api.uclassify.com/1/ResponseSchema}classification' at 0x1011ec4d0>
<Element '{http://api.uclassify.com/1/ResponseSchema}class' at 0x1011ec510>
<Element '{http://api.uclassify.com/1/ResponseSchema}class' at 0x1011ec550>
<Element '{http://api.uclassify.com/1/ResponseSchema}class' at 0x1011ec590>


搜索标记和属性的最简单方法是什么,例如在BeautifulSoup模块中?例如,如何轻松检索类元素的className和p属性?我一直在阅读关于lxml、xml.dom.minidom和ElementTree的不同内容,但我肯定错过了一些东西,因为我似乎无法得到我想要的东西。

首先
uclassify
是根节点,因此如果您只打印上面的
tree
,您将看到:

>>> tree
<Element '{http://api.uclassify.com/1/ResponseSchema}uclassify' at 0x101f56410>
例如,要获得3个
标记,您需要:

classify = readCalls.find('{%s}classify' % (xmlns,))
classification = classify.find('{%s}classification' %(xmlns,))
classes = classification.findall('{%s}classes'%(xmlns,))

首先,
uclassify
是根节点,因此如果您只需在上面打印
,您将看到:

>>> tree
<Element '{http://api.uclassify.com/1/ResponseSchema}uclassify' at 0x101f56410>
例如,要获得3个
标记,您需要:

classify = readCalls.find('{%s}classify' % (xmlns,))
classification = classify.find('{%s}classification' %(xmlns,))
classes = classification.findall('{%s}classes'%(xmlns,))
classify = readCalls.find('{%s}classify' % (xmlns,))
classification = classify.find('{%s}classification' %(xmlns,))
classes = classification.findall('{%s}classes'%(xmlns,))