获取艺术家姓名

时间:2018-03-30 20:37:32

标签: python last.fm pylast

我试图用pylast(https://github.com/pylast/pylast)来获取我上周三位前艺术家的名字,但是我遇到了一个错误,或者我得到了结果,我不知道我做错了什么。 pylast是Last.fm的Python接口。

我的代码:

import pylast

API_KEY = ""
API_SECRET = ""

username = ""
password_hash = pylast.md5("")

network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash)

user = network.get_authenticated_user();

weekly_artists = user.get_weekly_artist_charts();

# Keep the first three artists.
del weekly_artists[3:]

# Print the artist name and number of songs(weight).
for weekly_artist in weekly_artists:
  artist,weight = weekly_artist

  print (artist.get_name())
  print (artist.get_correction())

artist.get_name()返回

None

artist.get_correction()返回

Traceback (most recent call last):
  File "G:\projects\python\lastfm_weekly\lastfm-weekly.py", line 28, in <module>
    print (artist.get_correction())
  File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 1585, in get_correction
    self._request(self.ws_prefix + ".getCorrection"), "name")
  File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 1029, in _request
    return _Request(self.network, method_name, params).execute(cacheable)
  File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 744, in __init__
    network._get_ws_auth()
AttributeError: 'str' object has no attribute '_get_ws_auth'

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是一个快速而肮脏的解决方案,我确信有人会提供更好的东西,但我只是安装了测试包并且它有效。

network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)

artists = network.get_top_artists()
del artists[:3]

for i in artists:
    artist, weight = i

    print('Artist = {}. Weight = {}'.format(artist, weight))

我对这个软件包并不是很熟悉,我只是安装它来帮助解决这个问题,但我确实想知道“get_name()”和“get_correction()”是什么,因为它们不在你提供的代码中。< / p>

如果它们不是您创建的函数/在您的代码中定义的那么我会在那里查找问题。

此外,您正在对用户进行身份验证,但文档明确声明您不需要,除非您正在编写数据。