DEV Community

Cover image for How to scrape MusixMatch to get lyrics of your favourite songs in python.
Stɑrry Shivɑm
Stɑrry Shivɑm

Posted on • Edited on

How to scrape MusixMatch to get lyrics of your favourite songs in python.

Everyone loves music, I do aswell, so let's write a script today to fetch lyrics of your favourite songs from musixmatch.com. All you need is some basic knowledge of python.

We'll use two libraries, requests and BeautifulSoup4 you can install them with following command.

pip install beautifulsoup4 requests
Enter fullscreen mode Exit fullscreen mode

Now let's import both of our newly installed libraries.

import requests
from bs4 import BeautifulSoup
Enter fullscreen mode Exit fullscreen mode

We'll need a User-Agent header to make successful request so let's define that first.

HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
Enter fullscreen mode Exit fullscreen mode

We'll take musixmatch song url through inbuilt input() function, let's define a variable in which we'll store the song link.

SONG_LINK = input("Please enter song link: ")
Enter fullscreen mode Exit fullscreen mode

Now let's write a helper function which takes song url and returns BeautifulSoup object.

def get_soup(url: str) -> BeautifulSoup:
    """
    Utility function which takes a url and returns a Soup object.
    """
    response = requests.get(url, headers=HEADERS)
    soup = BeautifulSoup(response.text, "html.parser")
    return soup
Enter fullscreen mode Exit fullscreen mode

Let's atart scraping!
We need to find html class called lyrics__content__ok and fetch text content from it, then join those texts using newlines. If for some reason we couldn't find that class we'll look for lyrics__content__warning class instead and use Bs4's get_text() method to get lyrics content. Here's the code example:

# build bs4 soup object.                
soup = get_soup(SONG_LINK)
# find the lyrics data.
cols = soup.findAll(class_="lyrics__content__ok", text=True)
if cols:
    lyrics = "\n".join(x.text for x in cols)
elif data := soup.find(class_="lyrics__content__warning", text=True):
    lyrics = data.get_text()
# finally print the lyrics.
print(lyrics)
Enter fullscreen mode Exit fullscreen mode

That's it! Your script to get lyrics of your favourite songs from musixmatch is now complete. Here's the complete code.

import requests
from bs4 import BeautifulSoup

HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
SONG_LINK = input("Please enter song link: ")


def get_soup(url: str) -> BeautifulSoup:
    """
    Utility function which takes a url and returns a Soup object.
    """
    response = requests.get(url, headers=HEADERS)
    soup = BeautifulSoup(response.text, "html.parser")
    return soup


# build bs4 soup object.
soup = get_soup(SONG_LINK)
# find the lyrics data.
cols = soup.findAll(class_="lyrics__content__ok", text=True)
if cols:
    lyrics = "\n".join(x.text for x in cols)
elif data := soup.find(class_="lyrics__content__warning", text=True):
    lyrics = data.get_text()
# finally print the lyrics.
print(lyrics)
Enter fullscreen mode Exit fullscreen mode

And here's how to run it:

❯ python3 lyrics.py
Please enter song link: https://www.musixmatch.com/lyrics/Kygo-feat-Imagine-Dragons/Born-To-Be-Yours
I know I′ve given up
A hundred times before
But I know a miracle
Is not something to ignore
You take me for a fool
You take me for a fool

I never knew anybody 'til I knew you, ooh
I never knew anybody ′til I knew you, ooh
And I know when it rains, oh, it pours
And I know I was born to be yours
Enter fullscreen mode Exit fullscreen mode

I haven't showed full output containing whole lyrics due to obvious reasons. Hope you enjoyed this small article. ✨

Top comments (0)