Reputation: 117
Im getting the top artists from a specific country and then I want to store the name and tags of each artist.The name is working fine but the tags its not working. The tags are the genre like rock, jazz and so on.
Part where im getting the tags:
for child in tree:
for artist in child:
print(artist)
for tag in artist.findall('tags'):
print(tag)
bands[i]['Tags'] = tag.text
But its not working the print(tag) returns:
<Element 'name' at 0x00000211BBEB0F98>
<Element 'tags' at 0x00000211BBEBD638>
Do you know how to get and sore the tags in the bands{}? For exemple in the exemple above the tags are rock, classic rock, irish, pop and alternative.
The response have this format:
<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>
Minimal verifiable example:
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")
Upvotes: 0
Views: 121
Reputation: 9010
In your loop, artist.findall('tags')
returns a list with a single Element - the <tags>
element. You are trying to iterate over each <tag>
within the <tags>
Element. Use the following instead:
for tag in artist.find('tags').findall('tag')
Also, note that tag.text
is going to be None
. Instead you probably want tag.find('name').text
and tag.find('url').text
.
Upvotes: 2