Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:37:58+00:00 2026-05-29T06:37:58+00:00

I am trying to write a small script for interacting with the last.fm API.

  • 0

I am trying to write a small script for interacting with the last.fm API.

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.

Class for interacting with 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

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)

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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:37:59+00:00Added an answer on May 29, 2026 at 6:37 am

    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')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a small script to change the current directory to my
I have been trying to write a small app with its own option windows.
I've been trying to use Django-appengine to write a small web app, but I
Im trying to write a small class to better understand bit flags in c++.
I'm trying to write a small ruby script that detects if a given argument
I'm trying to write a small batch script that will delete files old files
I'm trying to write a small Python script to parse the .strings file in
I'm trying to write a small Python script that will get query results from
I am trying to write a small PHP script for managing subscriptions for a
I am trying to write a small python script to rename a bunch of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.