Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取每个标签名的一个";标签;xml响应的元素不工作_Python_Xml - Fatal编程技术网

Python 获取每个标签名的一个";标签;xml响应的元素不工作

Python 获取每个标签名的一个";标签;xml响应的元素不工作,python,xml,Python,Xml,我从一个特定的国家获得顶级艺术家,然后我想储存每个艺术家的名字和标签。名字很好用,但标签不起作用。标签是摇滚、爵士乐等流派 我得到标签的部分: for child in tree: for artist in child: print(artist) for tag in artist.findall('tags'): print(tag) bands[i]['Tags'] = tag.text 但打印(标

我从一个特定的国家获得顶级艺术家,然后我想储存每个艺术家的名字和标签。名字很好用,但标签不起作用。标签是摇滚、爵士乐等流派

我得到标签的部分:

for child in tree:
    for artist in child:
        print(artist)
        for tag in artist.findall('tags'):
            print(tag)
            bands[i]['Tags'] = tag.text
但打印(标记)返回时不起作用:

<Element 'name' at 0x00000211BBEB0F98>
<Element 'tags' at 0x00000211BBEBD638>
<lfm status="ok">
<artist>
<name>U2</name>
<tags>
<tag>
<name>rock</name>
<url>https://www.last.fm/tag/rock</url>
</tag>
<tag>
<name>classic rock</name>
<url>https://www.last.fm/tag/classic+rock</url>
</tag>
<tag>
<name>irish</name>
<url>https://www.last.fm/tag/irish</url>
</tag>
<tag>
<name>pop</name>
<url>https://www.last.fm/tag/pop</url>
</tag>
<tag>
<name>alternative</name>
<url>https://www.last.fm/tag/alternative</url>
</tag>
</tags>

</artist>
</lfm>
import xml.etree.ElementTree as ET
import requests

ID = 1

api_key = "b088cbedecd40b35dd89e90f55227ac2"  # generated for the example

bands = {}

# GET TOP ARTISTS

artistslist = requests.get(
    'http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&page=1&limit=5&api_key=' + api_key)
tree = ET.fromstring(artistslist.content)
for child in tree:
    for artist in child.findall('artist'):
        name = artist.find('name').text
        bands[ID] = {}
        bands[ID]['ID'] = ID
        bands[ID]['Name'] = name

        ID += 1

# GET ARTIST INFO
for i, v in bands.items():

    chosen = bands[i]['Name'].replace(" ", "+")
    artist = requests.get(
        'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=' + chosen + '&api_key=' + api_key)
    tree = ET.fromstring(artist.content)
    for child in tree:
        for artist in child:
            print(artist)
            for tag in artist.findall('tags'):
                print(tag['name'])
                bands[i]['Tags'] = tag.text
            if (artist.get('size') == "large"):
                if (artist.text is not None):
                    bands[i]['Image'] = artist.text

    print(bands[i]['Name'] + " RETRIEVED")

在循环中,
artist.findall('tags')
返回一个包含单个元素的列表,即
元素。您正试图迭代
元素中的每个
。请改用以下方法:

for tag in artist.find('tags').findall('tag')

另外,请注意,
tag.text
将是
None
。相反,您可能希望
tag.find('name').text
tag.find('url').text

在循环中,
artist.findall('tags')
返回一个包含单个元素的列表—
元素。您正试图迭代
元素中的每个
。请改用以下方法:

for tag in artist.find('tags').findall('tag')

另外,请注意,
tag.text
将是
None
。相反,您可能需要
tag.find('name').text
tag.find('url').text

…顺便说一句,如果您只是在代码中硬编码响应的最小必要子集,那么这将更接近于的最小部分——这样人们就不需要自己注册API密钥,也不需要希望这里的密钥仍然有效。(类似地,如果问题只存在于获取单个艺术家详细信息的代码中,MCVE没有理由包括吸引顶级艺术家的代码;即使您要查询公共API,也可以只硬编码一个艺术家名称,并完全删除第一个循环)…顺便说一句,如果您只是在代码中硬编码响应的最小必要子集,那么这将更接近于符合的最小部分-这样人们就不需要自己注册API密钥,也不需要希望这里的密钥仍然有效。(类似地,如果问题只存在于获取单个艺术家详细信息的代码中,那么MCVE没有理由包括吸引顶级艺术家的代码;即使您要查询公共API,也可以只硬编码一个艺术家名称并完全删除第一个循环)。谢谢,但看起来是这样的:回溯(最后一次调用):文件“C:/Users/Ozzy/PycharmProjects/getData/getData.py”,第19行,用于artist.find('tags').findall('tag'):AttributeError:'NoneType'对象在“for artist.find('tags').findall('tag'):”树中的child的
行和child中的artist的
行将迭代其父级的所有子级。您确定这就是您要做的吗?我想在json文件中存储所有artista信息,如下所示:{“ID”:0,“name”:“U2”,“url”:“,”标记“[“rock”,“classic rock”,“irish”,“pop”,“alternative”]}。使用xml,这样做似乎是正确的,因为名称和其他数据正在工作,只有标记不起作用。谢谢,但这样做似乎是:回溯(最近一次调用):文件“C:/Users/ozy/PycharmProjects/getData/getData.py”,第19行,在artist.find('tags')中为标记。findall('tag')):AttributeError:“非类型”对象在“艺术家中的标记”中没有“findall”属性。查找('tags')。findall('tag'):“树中的子对象的
行和子对象中的艺术家的
行将迭代其父对象的所有子对象。您确定这是您要执行的操作吗?我想将所有艺术家信息存储在json文件中,如下所示:{“ID”:0,“名称”:“U2”,“url”:“,”标记“:[“摇滚乐”,“古典摇滚乐”,“爱尔兰乐”,“流行乐”,“另类”]}。使用xml,这样做似乎是正确的,因为名称和其他数据正在工作,只有标记不起作用。