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

问题描述

我试图写一个python脚本做一个查询,Last.fm,但我不断收到一个无效的方法错误返回。

我不希望链接pre-书面last.fm Python库,我想这样做试验我所知道的之类的项目。在此先感谢!

 进口的urllib
给出import httplibPARAMS = urllib.urlen code({'法':'artist.getsimilar',
               艺术家:乐队,
               '限制':'5',
               API_KEY:#API重点放在这里})标题= {用户代理:的myapp / 1.0}LastFM等= httplib.HTTPConnection(ws.audioscrobbler.com)lastfm.request(POST,/ 2.0 /?参数,可以头)响应= lastfm.getresponse()
打印response.read()


解决方案

您缺少内容类型作为您的要求:应用程序/ x-WWW的形式urlen codeD 。这工作:

 进口的urllib
给出import httplibPARAMS = urllib.urlen code({'法':'artist.getsimilar',
               艺术家:乐队,
               '限制':'5',
               API_KEY:#API重点放在这里'})标题= {用户代理:的myapp / 1.0,
          内容类型:应用程序/ x-WWW的形式urlen codeD}LastFM等= httplib.HTTPConnection(ws.audioscrobbler.com)lastfm.request(POST,/ 2.0 /?参数,可以头)响应= lastfm.getresponse()
打印response.read()

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

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()
解决方案

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无效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:33