2

我正在尝试编写一个 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()
4

2 回答 2

2

您的请求缺少Content-type : "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()
于 2010-12-06T09:50:11.703 回答
0

Last.fm artist.getSimilar API 方法不需要 POST,它可以通过 GET 完成。

只有更改数据的 API 方法需要 POST 方法。

于 2011-01-29T21:44:14.563 回答