简体   繁体   English

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

[英]Python: Get songs Lyrics from genius - Error

I am working on getting the songs lyrics from genius using API.我正在努力使用 API 从天才那里获取歌曲歌词。 I am having an issue with extract titles and lyrics from JSON file once it is saved.保存后,我在从 JSON 文件中提取标题和歌词时遇到问题。 please see my code below.请在下面查看我的代码。

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']]

The error I am having is:我遇到的错误是:

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

Your help will be much appreciated.您的帮助将不胜感激。 Thank you in advance!先感谢您!

Regards,问候,

viku维库

The query output is saved to a json (or txt ) file, ie:查询输出保存到一个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...

But you can also use:但您也可以使用:

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)

References:参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM