Getting Lyrics of Songs via Python (LyricsGenius)

Batu YILDIRIM
Analytics Vidhya
Published in
4 min readFeb 3, 2021

--

LyricsGenius is a python library which makes easier to download any lyrics from Genius.com with its API.

Before starting to code, API key from genius is necessary. So, creating API is easy via this link.

After creating API, let’s start coding.

LyricsGenius works with Python 3. For installation,

First, check your python version.

python --version

Second, download the python library.

#If python --version is Python3, 
pip install lyricsgenius
#If python --version is Python2,
pip3 install lyricsgenius

For latest version, try to install with Github link.

pip install git+https://github.com/johnwmillr/LyricsGenius.git

Now, you are ready to use lyricsgenius. Import library and modules.

#api_key is your TOKEN from Genius Developers. It will look like that.api_key = 
"XXX-XXXX7XXXXXYUeXXXXXMXXWsUXXXXX7BXXXXX3_XXXXXsY_aBXXXXXzXX"
genius = lg.Genius(api_key)

For extra specifications on Genius searchs, there are some parameters in the example under below.

genius = lg.Genius(api_key, skip_non_songs=True, excluded_terms=["(Remix)", "(Live)"], remove_section_headers=True)

Next step is searhing an artist’s lyrics.

#Searches all of "Pop Smoke"'s songs.
artist = genius.search_artist("Pop Smoke")
#Searches 5 of "Pop Smoke"'s songs.
artist = genius.search_artist("Pop Smoke",max_songs=5)
#Searches 5 of "Pop Smoke"'s songs via its Title.
artist = genius.search_artist("Pop Smoke",max_songs=5,sort="title")
# Sorts = popularity release_date title

Output of

artist = genius.search_artist(“Pop Smoke”,max_songs=5,sort=”title”)

is:

Searching for songs by Pop Smoke...

Song 1: "Dior"
Song 2: "Mood Swings"
Song 3: "Welcome to the Party"
Song 4: "For the Night"
Song 5: "Hello"

Reached user-specified song limit (5).
Done. Found 5 songs.

For seeing the songs:

songs = artist.songs
print(songs)

Output:

[('Dior', 'Pop Smoke'), ('Mood Swings', 'Pop Smoke'), ('Welcome to the Party', 'Pop Smoke'), ('For the Night', 'Pop Smoke'), ('Hello', 'Pop Smoke')]

For specific song insode of limited or nonlimited songs:

song = artist.song("Dior")
print(song)

Output:

"Dior" by Pop Smoke:
Traphouse Mob
Huh,  roll another one
Said  I'm never lackin', always pistol packing
With them automa...

For lyrics of “song” variable:

print(song.lyrics)

Output:

Traphouse Mob
Huh,  roll another one
Said  I'm never lackin', always pistol packing
With them automatics, we gon' send him to heaven
Wait, wait, wait, wait, ayy, ayy, woo (Aw, shit), huh
Oh,  you feelin' sturdy, huh? (You feelin' sturdy, man)
Shake  somethin'
Shake it, shake it, shake it

She  like the way that I dance
She like the way that I move
She like the way that I rock
She like the way that I woo
And  she let it clap for a nigga
(She let it clap for a nigga)
And she throw it back for a nigga
(Yeah, she throw it back for a nigga)
Mike Amiri, Mike Amiri
Billie Jean, Billie Jean (Uh)
Christian Dior, Dior
I'm up in all the stores
When it rains, it pours
She like the way I rrr
Mike Amiri, Mike Amiri
Billie Jean, Billie Jean (Uh)
Christian Dior, Dior
I'm up in all the stores
When it rains, it pours
She like the way I rrr

When I walk in the spot, thirty on me
Buy out the club, niggas know that I'm paid
Bitch, I'm a thot, get me lit
I can't fuck with these niggas 'cause niggas is gay
All on my page suckin' dick
All in my comments and screamin' my name
While I'm in the club, throwin' them hundreds and fifties and ones and ones
Pop Smoke, they know I'm wildin'
If I'm on the island, I'm snatchin' the cell
Brody got locked, denied his bail
Until he free, I'm raisin' hell
Tell my shooters call me FaceTime
For all the times we had to face time
Free D-Nice, he doin' state time
If you need the glizzy, you could take mine
Please don't come out your mouth, you know I'm like that
I'll make a movie like TNT
Glock-30 on me, ask who really want it
I bet I air it like BNB
Nappy Blue wildin' in my section
And I keep that .38 for the weapon
Remember when I came home from corrections
All the bad bitches in my direction

She like the way that I dance
She like the way that I move
She like the way that I rock
She like the way that I woo
And she let it clap for a nigga
(She let it clap for a nigga)
And she throw it back for a nigga
(Yeah, she throw it back for a nigga)
Mike Amiri, Mike Amiri
Billie Jean, Billie Jean (Uh)
Christian Dior, Dior
I'm up in all the stores
When it rains, it pours
She like the way I rrr
Mike Amiri, Mike Amiri
Billie Jean, Billie Jean (Uh)
Christian Dior, Dior
I'm up in all the stores
When it rains, it pours
She like the way I rrr

Full Usage of Code Example

def get_lyrics(artists, song_limit):
c = 0
for artist in artists:
try:
songs = (genius.search_artist(artist, max_songs=song_limit, sort='popularity')).songs
song_lyrics_list = [song.lyrics for song in songs]
file = open(f"{artist}_lyrics_of_{len(song_lyrics_list)}_songs.txt", "w", encoding='utf-8')
file.write("\n \n \n \n \n".join(song_lyrics_list))
c += 1
print(f"Songs grabbed:{len(song_lyrics_list)}")
file.close()
print("___________________________________________________\n\n\n\n")
except:
print(f"some exception at {artist}: {c}")
# Sorts = popularity release_date titleapi_key = "YOUR_API_KEY"
import lyricsgenius as lg
genius = lg.Genius(api_key, skip_non_songs=True, excluded_terms=["(Remix)", "(Live)"], remove_section_headers=True)art = ["Pop Smoke","Weekend"]
lyrics = get_lyrics(art, 5)
#

Thanks for reading this article. I hope it will be helpful to you.

--

--

Batu YILDIRIM
Analytics Vidhya

Electric Electronic Engineer. Skilled in working on analog design, PCB design, C++ and Python also starts to learn IoT and machine learning applications.