Python: 获取天才歌曲歌词 - 错误

Python: Get songs Lyrics from genius - Error

我正在努力使用 API 从天才那里获取歌曲歌词。保存后,我在从 JSON 文件中提取标题和歌词时遇到问题。请在下面查看我的代码。

import lyricsgenius as genius
api=genius.Genius('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
artist=api.search_artist('Beyonce') #max_songs=3, sort="title"
aux=artist.save_lyrics(filename='artist.txt',overwrite=True,skip_duplicates=True,verbose=True)
titles=[song['title'] for song in aux['songs']]
lyrics=[song['lyrics'] for song in aux['songs']]

我遇到的错误是:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-4a24319b20b5> in <module>
----> 1 titles=[song['title'] for song in aux['songs']]
      2 lyrics=[song['lyrics'] for song in aux['songs']]

TypeError: 'NoneType' object is not subscriptable

非常感谢您的帮助。提前致谢!

此致,

维库

查询输出保存到json(或txt)文件,即:

import json
import lyricsgenius as genius

api=genius.Genius('xxx')
artist=api.search_artist('Pink Floyd', max_songs=1) #max_songs=3, sort="title"
aux=artist.save_lyrics(filename='artist.json',overwrite=True,verbose=True)

with open("artist.json") as f:
    j = json.load(f)

# do something with j...

但您也可以使用:

artist = api.search_artist("Andy Shauf", max_songs=3, sort="title")
print(artist.songs)
song = api.search_song("To You", artist.name)
print(song.lyrics)

参考文献:

  1. https://github.com/johnwmillr/LyricsGenius#usage
  2. https://docs.genius.com/