Appendix¶
This is a Python base notebook
Demo of scrapping lyrics for the first 10 songs using pyLyrics2¶
from pylyrics2 import extract_lyrics as pl
from pylyrics2 import clean_text as ct
import pandas as pd
import re
import sys, os
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
spotify_df = pd.read_csv('data/spotify_data.csv', index_col = 0 )
spotify_df[["song_title", "artist"]].head()
| song_title | artist | |
|---|---|---|
| 0 | Mask Off | Future |
| 1 | Redbone | Childish Gambino |
| 2 | Xanny Family | Future |
| 3 | Master Of None | Beach House |
| 4 | Parallel Lines | Junior Boys |
def scrapLyrics(song_title, artist):
with HiddenPrints():
try:
return pl.extract_lyrics(song_title, artist)
except:
return " "
lyrics = []
for i in range(10): # range(len(spotify_df)):
song_title = ct.clean_text(text=spotify_df.iloc[i]["song_title"], bool_contra_dict=False)
artist = ct.clean_text(text=spotify_df.iloc[i]["artist"], bool_contra_dict=False)
lyric = scrapLyrics(song_title, artist)
if lyric.strip() != "":
clean_lyric = ct.clean_text(lyric)
else:
clean_lyric = ""
lyrics.append([song_title, artist, clean_lyric])
pd.DataFrame(lyrics, columns = ["song_title", "artist", "lyrics"]).head(10)
| song_title | artist | lyrics | |
|---|---|---|---|
| 0 | mask off | future | introcall it how it is call it how it ishendri... |
| 1 | redbone | childish gambino | verse daylight i wake up feeling like you wo n... |
| 2 | xanny family | future | introthree exotic broads and i got em soakin p... |
| 3 | master of none | beach house | you always go to the parties to pluck the feat... |
| 4 | parallel lines | junior boys | if you found the words would you really say th... |
| 5 | sneakin | drake | intro drake amp skoolyyeah we got london on da... |
| 6 | childs play | drake | introbreaking news my niggas if your girlfrien... |
| 7 | gyngyhaj lny | omega | |
| 8 | ive seen footage | death grips | introget up beats bout waist deep swallowed by... |
| 9 | digital animal | honey claws | introhey honey claws honey claws honey claws j... |