- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我修改了smbrown.wordpress.com上发布的代码它可以使用 Last.fm API 提取热门轨道,如下所示:
#!/usr/bin/python
import time
import pylast
import re
from md5 import md5
user_name = '*******'
user_password = '*******'
password_hash = pylast.md5("*******")
api_key = '***********************************'
api_secret = '****************************'
top_tracks_file = open('top_tracks_wordle.txt', 'w')
network = pylast.LastFMNetwork(api_key = api_key, api_secret = api_secret, username = user_name, password_hash = password_hash)
# to make the output more interesting for wordle viz.
# run against all periods. if you just want one period,
# delete the others from this list
time_periods = ['PERIOD_12MONTHS', 'PERIOD_6MONTHS', 'PERIOD_3MONTHS', 'PERIOD_OVERALL']
# time_periods = ['PERIOD_OVERALL']
#####
## shouldn't have to edit anything below here
#####
md5_user_password = md5(user_password).hexdigest()
sg = pylast.SessionKeyGenerator(network) #api_key, api_secret
session_key = sg.get_session_key(user_name, md5_user_password)
user = pylast.User(user_name, network) #api_key, api_secret, session_key
top_tracks = []
for time_period in time_periods:
# by default pylast returns a seq in the format:
# "Item: Andrew Bird - Fake Palindromes, Weight: 33"
tracks = user.get_top_tracks(period=time_period)
# regex that tries to pull out only the track name (
# for the ex. above "Fake Palindromes"
p = re.compile('.*[\s]-[\s](.*), Weight: [\d]+')
for track in tracks:
m = p.match(str(track))
**track = m.groups()[0]** <-----------Here---------------
top_tracks.append(track)
# be nice to last.fm's servers
time.sleep(5)
top_tracks = "\n".join(top_tracks)
top_tracks_file.write(top_tracks)
top_tracks_file.close()
当脚本运行到“<------------这里----------------”标记的位置时,我收到一条错误消息: “...第 46 行,位于
track = m.groups()[0]
AttributeError:“NoneType”对象没有属性“groups””
我在这里呆了一天多了,不知道下一步该做什么。谁能给我一些关于这个问题的线索?
最佳答案
显然有些轨道名称与您的正则表达式不匹配,因此 match() 返回 None。捕获异常并检查track
。
关于python - pylast 软件从 Last.fm API 吸引 top_tracks 的一小段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501636/
我正在尝试使用 pylast 从 last_fm 获取我最近听过的 4 首歌曲。 到目前为止,我有这个代码,但操作系统只返回一首歌曲: def get_recents(self, max_result
专家, 我目前想在一个研究项目中使用所有歌曲的标签。当然,我手上有 Pylast。但是没有关于 Pylast 的详细文档。 那么,有没有人可以告诉我如何使用Pylast通过last.fm api获取歌
我是一个刚刚开始学习Python的完全菜鸟。我正在使用 Last.fm 的 pylast 库来提取用户邻居及其一些属性的列表,但是当我尝试打印邻居的国家/地区时,出现以下错误: thrillofme
我一直在使用 pylast.py 从 Last.fm 检索有关艺术家和事件的各种类型的信息。 但是,pylast 没有任何从事件中检索 field 信息的功能。特别是,如果您查看 event.getI
我修改了smbrown.wordpress.com上发布的代码它可以使用 Last.fm API 提取热门轨道,如下所示: #!/usr/bin/python import time impor
我正在尝试访问 pylast 的库类,但一定做错了什么。我可以让大多数其他功能正常工作。以下是一个代码示例,它仅采用标准工作示例,并添加了我认为将专辑添加到我的 last.fm 库的正确方法: imp
我是一名优秀的程序员,十分优秀!