How to download an artists’ lyrics from Genius.com using Python

  • Search requests allow you to search Genius.com for any given string, just like you would in the search box on the website.
  • The Song and Artist requests allow you to directly access an item from the Genius database by providing the item’s corresponding API ID. After receiving a request, the database returns a JSON object containing search results, song names, etc.

Import your modules and assign your credentials/artist

#Assign your Genius.com credentials and select your artist
import lyricsgenius as genius
geniusCreds = "{Credentials}"
artist_name = "{Your Chosen Artist}"
#Connect your credentials and chosen artist to the genius object then test the first 5 songs
api = genius.Genius(geniusCreds)
artist = api.search_artist(artist_name, max_songs=5)
artist = api.search_artist(artist_name)
import os
os.getcwd()

Save your songs

artist.save_lyrics()

Open your json file

import pandas as pd
Artist=pd.read_json("Lyrics_{ArtistName}.json")
Artist['songs']
Artist['songs'][5]['lyrics']

Extract relevant song data

#Create an empty dictionary to store your songs and related data
artist_dict = {}
def collectSongData(adic):
dps = list()
title = adic['title'] #song title
url = adic['raw']['url'] #spotify url
artist = adic['artist'] #artist name(s)
song_id = adic['raw']['id'] #spotify id
lyrics = adic['lyrics'] #song lyrics
year = adic['year'] #release date
upload_date = adic['raw']['description_annotation']['annotatable']['client_timestamps']['lyrics_updated_at'] #lyrics upload date
annotations = adic['raw']['annotation_count'] #total no. of annotations
descr = adic['raw']['description'] #song descriptions

dps.append((title,url,artist,song_id,lyrics,year,upload_date,annotations,descr)) #append all to one tuple list
artist_dict[title] = dps #assign list to song dictionary entry named after song title

collectSongData(Artist['songs'][5]) #check function works
artist_dict['{chosen song}'][0][4]

Store your data in a CSV file

import csvdef updateCSV_file():
upload_count = 0 #Set upload counter
location = "{suitable_file_location}" #Pick file location
print("input filename of song file, please add .csv")
filename = input() #give your file a name
file = location + filename
with open(file, 'w', newline='', encoding='utf-8') as file: #open a new csv file
a = csv.writer(file, delimiter=',') #split by comma
#(title,url,artist,song_id,lyrics,year,upload_date,annotations,descr)
headers = ["Title","URL","Artist", "Song ID", "Lyrics", "Year", "Upload Date", "Annotations","Description"] #create header row
a.writerow(headers) #add header row
for song in artist_dict:
a.writerow(artist_dict[song][0])
upload_count+=1

print(str(upload_count) + " songs have been uploaded")
updateCSV_file()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rare Loot

Rare Loot

Experimenting with Python and Social Media APIs using web scraping, exploratory data analysis and amateur coding.