Last.fm API 无效方法
我正在尝试写一个Python脚本,用来查询Last.fm,但是我总是收到一个无效方法的错误信息。
我不想要那些已经写好的Last.fm Python库的链接,我想把这个当作一个“测试我所知道的东西”的项目。提前谢谢你们!
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()
2 个回答
0
Last.fm的artist.getSimilar这个API方法不需要用POST请求,可以用GET请求来完成。
只有那些会改变数据的API方法才需要用POST请求。
2
你的请求缺少内容类型,应该是"application/x-www-form-urlencoded"。这样做就可以了:
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()