当前位置: 首页 > 面试题库 >

ElementTree findall()返回空列表

施茂
2023-03-14
问题内容

我正在尝试编写一个用于与last.fm API进行交互的小脚本。

我有一点使用的经验ElementTree,但是以前使用它的方式似乎无效,而是返回一个空列表。
我删除了API密钥,因为我不知道它到底应该有多私密,并举了一个示例,说明了我在该位置接收的XML。

与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

调用的get_now_playing方法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样本:

<?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可以在树下的任何地方找到。因此,下面的方法应该起作用,例如:

api_results = ElementTree.parse(response).findall('.//track')


 类似资料:
  • 有人能告诉我为什么列表返回空吗?我的xpath是准确的,因为我重新检查了它,但我仍然无法迭代它,而调试for循环甚至没有执行。我不确定我哪里出了问题。

  • 我试图用刮擦和飞溅来刮取衣服的图像和一些产品信息。我想得到的形象,只有产品(所以没有模型)。比如这张照片https://www2.hm.com/nl_nl/productpage.0220094001.html 然而,如果我试图让src在Scrapy shell中 回答xpath('//figure[包含(@class,“secondary”)]///img//@src')。摘录() 返回一个空列

  • 我们正在使用Spring 4.x 和 swagger-jersey2-jaxrs_2.10。Swagger 不会列出我的 API,它总是只返回版本详细信息。 pom.xml web.xml 资源类 ResourceConfig类 我的应用程序基路径/api-docs返回

  • 如果我跑: 我会得到一张空名单。我猜它与名称空间有关,但我不知道如何修复它。

  • 问题内容: 我在Android中有一个客户端应用程序,用于将文件发送到服务器。服务器使用Apache Commons FileUpload API来解析表单数据值。 该发送该请求: 服务器代码: 问题就在这里。返回的列表为空,我无法获取表单数据值。 问题答案: 如果您已经(隐式)预先解析了请求正文,则此位置将为空。HTTP请求正文只能被读取/解析 一次 (因为客户端仅发送一次,并且不会多次发送)。

  • 我正在尝试检索一个带有Android特定注释的类列表。我正在尝试使用反射库来完成此操作。但无论我做什么,Reflections都返回一个空集。最后,我尝试使用Reflections.getAllTypes(),它应该返回包中的所有类,它给我的消息是“Could not find subtypes of Object.nake SubTypesScanner initialized to inclu