0

我正在尝试将这样的网址(http://musicbrainz.org/ws/2/artist/72c536dc-7137-4477-a521-567eeb840fa8)导入 python 并提取“性别”的值。

import urllib2
import codecs
import sys
import os
from xml.dom import minidom
import xml.etree.cElementTree as ET

#urlbob = urllib2.urlopen('http://musicbrainz.org/ws/2/artist/72c536dc-7137-4477-a521-567eeb840fa8')
url = 'dylan.xml'

#attempt 1 - using minidom
xmldoc = minidom.parse(url)
itemlist = xmldoc.getElementsByTagName('artist') 

#attempt 2 - using ET
tree = ET.parse('dylan.xml')
root = tree.getroot()

for child in root:
    print child.tag, child.attrib 

我似乎无法通过迷你 dom 的东西或 etree 的东西来了解性别。以当前形式,脚本返回

{http://musicbrainz.org/ns/mmd-2.0#}artist {'type': 'Person', 'id': '72c536dc-7137-4477-a521-567eeb840fa8'}
4

2 回答 2

0

那是因为你循环root的只是树的根,这有意义吗?当你循环根时,它只会返回下一个孩子并停在那里。

您需要循环迭代,以便它返回下一个节点并获得结果,请参见:

tree = ET.parse('dylan.xml')
root = tree.getroot()

# loop the root iterable which will keep returning next node
for node in root.iter(): # or root.getiterator() if < Python 2.7
    print node.tag, node.attrib, node.text

结果:

{http://musicbrainz.org/ns/mmd-2.0#}metadata {} None
{http://musicbrainz.org/ns/mmd-2.0#}artist {'type': 'Person', 'id': '72c536dc-7137-4477-a521-567eeb840fa8'} None
{http://musicbrainz.org/ns/mmd-2.0#}name {} Bob Dylan
{http://musicbrainz.org/ns/mmd-2.0#}sort-name {} Dylan, Bob
{http://musicbrainz.org/ns/mmd-2.0#}ipi {} 00008955074
{http://musicbrainz.org/ns/mmd-2.0#}ipi-list {} None
{http://musicbrainz.org/ns/mmd-2.0#}ipi {} 00008955074
{http://musicbrainz.org/ns/mmd-2.0#}ipi {} 00008955172
{http://musicbrainz.org/ns/mmd-2.0#}isni-list {} None
{http://musicbrainz.org/ns/mmd-2.0#}isni {} 0000000121479733
{http://musicbrainz.org/ns/mmd-2.0#}gender {} Male
{http://musicbrainz.org/ns/mmd-2.0#}country {} US
{http://musicbrainz.org/ns/mmd-2.0#}area {'id': '489ce91b-6658-3307-9877-795b68554c98'} None
{http://musicbrainz.org/ns/mmd-2.0#}name {} United States
{http://musicbrainz.org/ns/mmd-2.0#}sort-name {} United States
{http://musicbrainz.org/ns/mmd-2.0#}iso-3166-1-code-list {} None
{http://musicbrainz.org/ns/mmd-2.0#}iso-3166-1-code {} US
{http://musicbrainz.org/ns/mmd-2.0#}begin-area {'id': '04e60741-b1ae-4078-80bb-ffe8ae643ea7'} None
{http://musicbrainz.org/ns/mmd-2.0#}name {} Duluth
{http://musicbrainz.org/ns/mmd-2.0#}sort-name {} Duluth
{http://musicbrainz.org/ns/mmd-2.0#}life-span {} None
{http://musicbrainz.org/ns/mmd-2.0#}begin {} 1941-05-24
于 2014-11-16T23:51:18.397 回答
0
## This prints out the tree as the xml lib sees it 
## (I found it made debugging a little easier)
#def print_xml(node, depth = 0):
#    for child in node:
#        print "\t"*depth + str(child)
#        print_xml(child, depth = depth + 1)
#print_xml(root)

# attempt 1
xmldoc = minidom.parse(url)
genders = xmldoc.getElementsByTagName('gender') # <== you want gender not artist
for gender in genders:
    print gender.firstChild.nodeValue

# attempt 2
ns = "{http://musicbrainz.org/ns/mmd-2.0#}"
xlpath = "./" + ns + "artist/" + ns + "gender"
genders = root.findall(xlpath) # <== xpath was made for this..
for gender in genders:
    print gender.text

所以..您第一次尝试的问题是您正在查看所有艺术家元素的列表,而不是性别元素(列表中唯一艺术家元素的子元素)。

第二次尝试的问题是您正在查看根元素的子元素列表(这是一个包含单个元数据元素的列表)。

底层结构是:

<artist>
    <name>
    <sort-name>
    <ipi>
    <ipi-list>
        <ipi>
        <ipi>
    <isni-list>
        <isni>
    <gender>
    <country>
    <area>
        <name>
        <sort-name>
        <iso-3166-1-code-list>
            <iso-3166-1-code>
    <begin-area>
        <name>
        <sort-name>
    <life-span>
        <begin>

所以你需要得到根 -> 艺术家 -> 性别,或者只是搜索你真正想要的节点(在这种情况下是性别)。

于 2014-11-17T00:28:34.227 回答