简体   繁体   English

用于歌词的 Python Twitter Bot - 使用 Lyricsgenius 包收集的歌词

[英]Python Twitter Bot for Lyrics - lyrics collected using lyricsgenius package

I am using the lyricsgenius python package ( https://pypi.org/project/lyricsgenius/ ) to make a Twitter Lyrics bot as per this tutorial - https://medium.com/@mahibhosain98/creating-a-lyrics-bot-on-twitter-with-python3-and-aws-lambda-1e22743dc3b7我正在使用lyricsgenius python包( https://pypi.org/project/lyricsgenius/ )按照本教程制作一个Twitter歌词机器人 - https://medium.com/@mahibhosain98/creating-a-lyrics-bot -on-twitter-with-python3-and-aws-lambda-1e22743dc3b7

So far my app successfully returns the lyrics for a randomly selected song by the specified artist using the following code:到目前为止,我的应用程序使用以下代码成功返回了指定艺术家随机选择的歌曲的歌词:

all_songs = ["array_of_song_titles"]

def get_raw_lyrics():
    genius_client_access_token = "my_access_token"
    genius = lyricsgenius.Genius(genius_client_access_token)
    random_song_title = random.choice(all_songs)
    lyrics = genius.search_song(random_song_title, "Artist Name").lyrics
    song = random_song_title.upper()
    return lyrics, song

lyrics, song = get_raw_lyrics()

print(lyrics)

However, when I print the lyrics, I can see that after the final lyric it also picks up 'EmbedShare URLCopyEmbedCopy' from genius.com.但是,当我打印歌词时,我可以看到在最后的歌词之后,它还会从 Genius.com 中选择“EmbedShare URLCopyEmbedCopy”。

For example the final line would look like例如,最后一行看起来像

'Dancing to electro-pop like a robot from 1984
Said, from 1984EmbedShare URLCopyEmbedCopy'

How do I prevent EmbedShare URLCopyEmbedCopy from appearing in the final lyric string?如何防止 EmbedShare URLCopyEmbedCopy 出现在最终歌词字符串中?

A simple fix is to not print them with string indexing一个简单的解决方法是不使用字符串索引打印它们

print(lyrics[:-27])

Since the length of the string is 27, so we don't want the last 27 characters由于字符串的长度是27,所以我们不想要最后27个字符

print(lyrics.replace("EmbedShare URLCopyEmbedCopy", ""))适用于您的用例?

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

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