ElementTree findall()返回空列表 [英] ElementTree findall() returning empty list
问题描述
我正在尝试编写一个用于与last.fm API进行交互的小脚本.
I am trying to write a small script for interacting with the last.fm API.
我对使用ElementTree
有一点经验,但是我以前使用它的方式似乎无效,而是返回一个空列表.
我删除了API密钥,因为我不知道它到底应该有多私密,并举了一个我在原处接收XML的示例.
I have a small bit of experience working with ElementTree
, but the way I used it previously doesn't seem to be working, it instead returns an empty list.
I removed the API key as I don't know exactly how private it should be, and gave an example of the XML I am receiving in it's place.
与API交互的类:
from xml.etree import ElementTree
import urllib
import urllib2
class Last_fmWrapper(object):
def __init__(self):
self.last_fm_api_key = '*****************************'
self.api_url = 'http://ws.audioscrobbler.com/2.0/'
def get_now_playing(self, last_fm_user, method):
self.last_fm_user = last_fm_user
self.method = method
parameters = {'user':self.last_fm_user, 'api_key':self.last_fm_api_key, 'method':self.method}
encoded_parameters = urllib.urlencode(parameters)
request = urllib2.Request(self.api_url, encoded_parameters)
response = urllib2.urlopen(request)
api_results = ElementTree.parse(response).findall('track')
# why does api_results == []?
def compare_tasteometer(self):
pass
def register_user(self):
pass
调用Last_fmWrapper()
的get_now_playing方法:
Call to get_now_playing method of Last_fmWrapper()
:
from last_fm_wrapper import Last_fmWrapper
last_fm = Last_fmWrapper()
now_playing = last_fm.get_now_playing('BiriBiriRG', 'user.getRecentTracks')
if now_playing == None:
print 'You not currently playing anything.'
else:
print 'You are now playing {}'.format(now_playing)
我收到的xml样本:
Sample of xml I receive:
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<recenttracks user="BiriBiriRG" page="1" perPage="10" totalPages="18406" total="184058" >
<track>
<artist mbid="01809552-4f87-45b0-afff-2c6f0730a3be">Elvis Presley</artist>
<name>Thrill Of Your Love</name>
<streamable>1</streamable>
<mbid></mbid>
<album mbid="c445fa3a-3b24-41d4-b955-b6ca560c6f7a">Love Songs</album>
<url>http://www.last.fm/music/Elvis+Presley/_/Thrill+Of+Your+Love</url>
<image size="small">http://userserve-ak.last.fm/serve/34s/69037914.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64s/69037914.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/69037914.png</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/300x300/69037914.png</image>
<date uts="1328153196">2 Feb 2012, 03:26</date>
</track>
<track>
<artist mbid="efc8a006-d0c6-4a9b-8cb1-91ca770fa2b9">Colbie Caillat</artist>
<name>Oxygen</name>
<streamable>1</streamable>
<mbid></mbid>
<album mbid="2d297b29-a215-42fe-8a8c-dc8b502903b1">Coco</album>
<url>http://www.last.fm/music/Colbie+Caillat/_/Oxygen</url>
<image size="small">http://userserve-ak.last.fm/serve/34s/69229764.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64s/69229764.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/69229764.png</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/300x300/69229764.png</image>
<date uts="1328152962">2 Feb 2012, 03:22</date>
</track>
<track>
推荐答案
问题是findall
仅在元素被赋予标签名称的情况下才搜索该元素的直接后代.您需要为其提供一个XPath表达式,该表达式将在其下方树中的任何位置找到track
.因此,下面的方法应该起作用,例如:
The problem is that findall
only searches the immediate descendants of an element if it is given a tag name. You need to give it an XPath expression that will find track
anywhere in the tree beneath it. So the following should work, for example:
api_results = ElementTree.parse(response).findall('.//track')
这篇关于ElementTree findall()返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!