The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Scrape lyrics by artist and album

To scrape lyrics by artist and album, you can use Python with libraries like requests, BeautifulSoup, and optionally pandas to organize data. However, many lyrics websites have terms of service that prohibit scraping, so use APIs where possible (like Genius API) or make sure you have permission to scrape.

Here’s an example using the Genius API, which is one of the most reliable ways to access lyrics:


Step 1: Set Up the Genius API

  1. Get an access token from Genius API.

  2. Install the required Python libraries:

bash
pip install requests beautifulsoup4

Step 2: Python Script to Scrape Lyrics by Artist and Album

python
import requests from bs4 import BeautifulSoup GENIUS_API_TOKEN = 'YOUR_GENIUS_ACCESS_TOKEN' BASE_URL = 'https://api.genius.com' def get_album_songs(artist_name, album_name): headers = {'Authorization': f'Bearer {GENIUS_API_TOKEN}'} search_url = f"{BASE_URL}/search" params = {'q': f'{album_name} {artist_name}'} response = requests.get(search_url, params=params, headers=headers) json_data = response.json() songs = [] for hit in json_data['response']['hits']: if artist_name.lower() in hit['result']['primary_artist']['name'].lower(): songs.append({ 'title': hit['result']['title'], 'url': hit['result']['url'] }) return songs def scrape_lyrics(url): page = requests.get(url) soup = BeautifulSoup(page.text, 'html.parser') lyrics_div = soup.find('div', class_='lyrics') # Old format if lyrics_div: return lyrics_div.get_text(strip=True) # Newer Genius page format lyrics_divs = soup.select('div[data-lyrics-container="true"]') lyrics = 'n'.join([div.get_text(strip=True, separator='n') for div in lyrics_divs]) return lyrics def get_album_lyrics(artist_name, album_name): songs = get_album_songs(artist_name, album_name) album_lyrics = {} for song in songs: try: lyrics = scrape_lyrics(song['url']) album_lyrics[song['title']] = lyrics print(f"Lyrics scraped for: {song['title']}") except Exception as e: print(f"Failed to get lyrics for {song['title']}: {e}") return album_lyrics # Example usage artist = 'Coldplay' album = 'Parachutes' lyrics_dict = get_album_lyrics(artist, album) for title, lyrics in lyrics_dict.items(): print(f"n=== {title} ===n{lyrics}n")

Notes:

  • Replace 'YOUR_GENIUS_ACCESS_TOKEN' with your actual Genius API token.

  • Genius API search doesn’t return albums directly, so this uses a heuristic search by combining album and artist name.

  • For full album support, consider combining this with manual mapping or scraping of tracklists from Wikipedia (using wikipedia module or similar).

  • Scraping Genius HTML requires fallback handling due to format changes.

Let me know if you want the script to save lyrics to a file or handle multiple albums automatically.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About