Scrape Spotify’s API within 20 mins

Alpar Gür
5 min readJul 2, 2023
Photo by Jonas Leupe on Unsplash

Recently, I worked on a project to develop a machine learning model which should predict the genre of a given track based on the audio features of it. In order to do that, we needed tracks and their audio features. Fortunately, Spotify offers various APIs to extract tracks and their preanalysed audio features.

In this post, I will be sharing with you how we collected the data to train our models. Without further ado, let’s get started!

Prerequisites

  • While calling Spotify’s APIs authentication is required. Therefore, you will need a client-id and client secret to obtain an authentication token. You will find these in settings tab of Spotify for Developers. If you didn’t used it before you can just click to the link and create a Spotify app.
  • For scraping the APIs we’ll use Python, pandas and Jupyter so make sure your development environment is fully equipped and up to date.

Authentication

First of all, create new environment variables using CLI and reload your environment:

$ export SPOTIFY_CLIENT_ID=<your-client-id>
$ export SPOTIFY_CLIENT_SECRET=<your-client-secret>

Code snippet below imports necessary libraries and defines a function which exposes Spotify API to get the authentication…

--

--

No responses yet