How To Scrape Data From Apps Like Billboard And Spotify Using Python?

May 16, 2022
Scrape Data From Apps Like Billboard

Spotify and Billboard are one of the world’s most widely used music streaming services. They also provide an API that allows developers to use their massive music library to create fascinating apps and gain insights into our listening habits.

In this blog, you will check how web scraping extracts the selected data from Billboard and Spotify.

The script below will consider a ‘year’ as input and will extract the Billboard top 100 songs website and create a playlist for you.

Logging in to the Spotify account if you are not registered previously.

Logging in to the Spotify Account

After you’ve logged in, visit the developer dashboard and build a new Spotify app using the link below.


Insert the Client ID and Client Secret from your Spotify app into your Python code. Add as your Referral URI by clicking on the App and then ‘Edit options’.

from bs4 import BeautifulSoup
import requests
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth

CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" #Enter your own ID
CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxx" #Enter your own Secret 

YEAR = input("Which year do you want to travel to? Type the year in this format YYYY starting from 2006: ")

URL = f"{YEAR}/hot-100-songs/"

response = requests.get(URL)
billboard_webpage = response.text

soup = BeautifulSoup(billboard_webpage, "html.parser")

songs ="li ul li h3")

songs_list = [song.getText() for song in songs]

res = [s.replace('\n\n\t\n\t\n\t\t\n\t\t\t\t\t', '') for s in songs_list]
result = [s.replace('\t\t\n\t\n', '') for s in res]

song_names = result

sp = spotipy.Spotify(

user_id = sp.current_user()["id"]

#Searching Spotify for songs by title
song_uris = []
year = YEAR

for song in song_names:
result ="track:{song} year:{year}", type="track")
uri = result["tracks"]["items"][0]["uri"]
except IndexError:
print(f"{song} doesn't exist in Spotify. Skipped.")

#Creating a new private playlist in Spotify
playlist = sp.user_playlist_create(user=user_id, name=f"{YEAR} Billboard Top 100", public=False)

#Adding songs found into the new playlist
sp.playlist_add_items(playlist_id=playlist["id"], items=song_uris)

The application will take you to an ‘Example Domain’ website during execution, where you must copy the web URL and enter it into the console when prompted.

Only the Top 100 songs from 2006 are currently accessible. So, you put in 2006 or later as the year.

spotify web

The above-mentioned Python script will help you to easily extract Billboard or Spotify data using Python.

For more information, contact X-Byte Enterprise Crawling today or request for a quote!