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.

dashboard

Insert the Client ID and Client Secret from your Spotify app into your Python code. Add http://example.com 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"https://www.billboard.com/charts/year-end/{YEAR}/hot-100-songs/"

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

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

songs = soup.select(selector="li ul li h3")

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

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
print(result)

sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
scope="playlist-modify-private",
redirect_uri="http://example.com/",
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
show_dialog=True,
cache_path="tokenNew.txt"
)
)

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

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

for song in song_names:
result = sp.search(q=f"track:{song} year:{year}", type="track")
print(result)
try:
uri = result["tracks"]["items"][0]["uri"]
song_uris.append(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)
print(playlist)

#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!