last.fm api 无效方法 [英] Last.fm api invalid method

查看:25
本文介绍了last.fm api 无效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 python 脚本来对 Last.fm 进行查询,但我不断收到返回无效方法错误.

I am trying to write a python script to do a query to Last.fm, but I keep getting an invalid method error returned.

我不想链接到预先编写的 last.fm python 库,我试图将其作为测试我所知道的"类型的项目来执行.提前致谢!

I don't want links to pre-written last.fm python libraries, I am trying to do this as a "test what I know" kind of project. Thanks in advance!

import urllib
import httplib

params = urllib.urlencode({'method' : 'artist.getsimilar',
               'artist' : 'band',
               'limit' : '5',
               'api_key' : #API key goes here})

header = {"user-agent" : "myapp/1.0"}

lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")

lastfm.request("POST","/2.0/?",params,header)

response = lastfm.getresponse()
print response.read()

推荐答案

您缺少 Content-type 来满足您的要求:"application/x-www-form-urlencoded".这有效:

You lack Content-type for your request: "application/x-www-form-urlencoded". This works:

import urllib
import httplib

params = urllib.urlencode({'method' : 'artist.getsimilar',
               'artist' : 'band',
               'limit' : '5',
               'api_key' : '#API key goes here'})

header = {"user-agent" : "myapp/1.0",
          "Content-type": "application/x-www-form-urlencoded"}

lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")

lastfm.request("POST","/2.0/?",params,header)

response = lastfm.getresponse()
print response.read()

这篇关于last.fm api 无效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆