You can now get lyrics for a song just by passing in the song name without passing an artist name and this library will do the rest of the job for you.
It fetches, extracts and returns the song's title and song lyrics from various websites autocorrecting the song names for the misspelled names along the way.
While I was searching for a Python library to extract lyrics for songs to integrate it into my WhatsApp and Slack chatbot, I couldn't find any libraries or APIs which could only accept song names. The APIs and libraries I tested required the accurate spelling of song names and artist name to be passed in for fetching the song lyrics. And even after passing all of these information, there were still some song lyrics which weren't available in their APIs and libraries.
This is the reason why I had to write an algorithm for fetching and scraping song lyrics from various websites even for any misspelled song name passed-in by the user.
pip install lyrics-extractor
-
You will need an API Key and Engine ID of Google Custom Search JSON API.
-
Create your new Custom Search Engine here to get your Engine ID: https://cse.google.com/cse/create/new
-
Add any of the following or all websites as per your choice in your Custom Search Engine:
-
You are free to customise your Custom Search Engine by prioritising any of your preferred keywords, excluding any web pages or turning on the 'Safe Search' feature.
NOTE: Please don't turn on the 'Search the entire Web' feature as it is currently not possible to scrape from any random sites appearing in the search results.
-
Visit here to get your API key: https://developers.google.com/custom-search/v1/overview
-
-
Python 3 installed on your machine.
-
BeautifulSoup, Requests and lxml library.
NOTE: You don't need to install these libraries seperately when you install Lyrics Extractor using
pip
but if are cloning this repository then you may usepython setup.py install
to install all the project dependencies.
-
Import this module in your Python program.
from lyrics_extractor import SongLyrics
-
Assign a Variable to the Class by passing in the Google Custom Search JSON API key and Engine ID received after creating a custom search engine.
extract_lyrics = SongLyrics(GCS_API_KEY, GCS_ENGINE_ID)
Replace 'GCS_API_KEY' and 'GCS_ENGINE_ID' with the API key and Engine ID received.
-
Get the title and the lyrics for the song by passing in the song name in the class function.
data = extract_lyrics.get_lyrics("Shape of You")
-
If you got the title and the lyrics for the song correctly then change the song name 'Shape of you' and try again with any misspelled song name like 'Shaep fo you'.
-
Voila! You have received your requested song lyrics even after passing in a misspelled song name.
- Fork this repository.
- Clone it onto your local machine and test if everything works correctly before making any changes.
- Make the appropriate changes.
- Test it.
- Test it again.
- If everything's fine, open a pull request.
We will be more than happy to review your Pull Requests. So go for it and contribute to this awesome open source community.
If your Pull Request is accepted, you will surely get credits here.
I have only been extracting lyrics for Educational Purposes. The chatbots I mentioned at the start are all being used for Non-commercial Purposes.
If you are planning to use this library for commercial purposes then do have a look at the terms and privacy policies of these websites and scrape lyrics only from sites which allow their content to be scraped and used commercially.
At the end I would say do respect the website's servers and please don't bombard with a lot of requests at once, else you are bound to get an error.