Spotipy API to scrape Spotify Data

부서진·2022년 10월 13일

Crawling

목록 보기
2/3

출처: https://machinelearningknowledge.ai/tutorial-how-to-use-spotipy-api-to-scrape-spotify-data/

Step 1: Creating Spotify Developers Account
기존에 사용하던 Spotify 계정을 사용하여 로그인

Step 2: Creating a New App

Step 3: Obtaining Client Id and Client Secret Keys

load the necessary libraries

pip install spotipy

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import json

import numpy as np
import pandas as pd

client_id and client_secret

client_id = <step 3의 32자 난수의 id 복사>
client_secret = <step 3의 32자 난수의 secret 복사>

client_credentials_manager = SpotifyClientCredentials(client_id=client_id,client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

Spotify playlist

# https://open.spotify.com/playlist/37i9dQZF1DX35DWKgAk2B5
# 위 url에서 37i9dQZF1DX35DWKgAk2B5 부분
playlist_id = <알고자 하는 playlist의 url의 난수 복사>
results = sp.user_playlist(<username>, playlist_id, 'tracks')

details of the track

playlist_tracks_data = results['tracks']
playlist_tracks_id = []
playlist_tracks_titles = []
playlist_tracks_artists = []
playlist_tracks_first_artists = []

for track in playlist_tracks_data['items']:
    playlist_tracks_id.append(track['track']['id'])
    playlist_tracks_titles.append(track['track']['name'])
    # adds a list of all artists involved in the song to the list of artists for the playlist
    artist_list = []
    for artist in track['track']['artists']:
        artist_list.append(artist['name'])
    playlist_tracks_artists.append(artist_list)
    playlist_tracks_first_artists.append(artist_list[0])

extract audio features

features = sp.audio_features(playlist_tracks_id)

feature_df = pd.DataFrame(data=features, columns = features[0].keys())

merge audio features and track information

features_df['title'] = playlist_tracks_titles
features_df['first_artist'] = playlist_tracks_first_artists
features_df['all_artists'] = playlist_tracks_artists
#features_df = features_df.set_index('id')
features_df = features_df[['id', 'title', 'first_artist', 'all_artists',
                           'danceability', 'energy', 'key', 'loudness',
                           'mode', 'acousticness', 'instrumentalness',
                           'liveness', 'valence', 'tempo',
                           'duration_ms', 'time_signature']]
                           
features_df

create dataset

features_df.to_csv("playlist.csv", encoding='utf-8',index="false")

4개의 댓글

comment-user-thumbnail
2023년 9월 25일

It's a valuable resource for developers interested in working with Spotify's data. If you're considering taking your interest in Spotify further and want to explore building a music streaming app, this guide on "how to make a Spotify app" can provide you with a comprehensive understanding of the development process and associated costs: https://www.cleveroad.com/blog/how-to-make-a-spotify-app--look-inside-a-spotify-app-and-find-out-the-cost-of-app-development/. It's a great complement to your data scraping work.

답글 달기
comment-user-thumbnail
2024년 8월 27일

Very good article, now i will go download spotify apk to experience free songs only available here
https://aloapk.com/es/spotify-apk/

답글 달기
comment-user-thumbnail
2025년 9월 12일

You don’t really need to “scrape” Spotify since Spotipy gives you proper API access for tracks, playlists, and artist data. Just register a Spotify developer app, grab your client ID/secret, and use Spotipy to authenticate. From there you can pull track metadata, audio features, or even build custom playlists programmatically. It’s much cleaner and safer than scraping. And if you’re just looking for a quick way to listen while testing, something simple like SpotifyFree.net
https://spotifyfree.net/

답글 달기
comment-user-thumbnail
2025년 9월 24일

thanks for sharing, i will find out if the Spotify API works well with the Spotify Mod APK app i downloaded from https://spotify-apk.es.modfyp.com.

답글 달기