0

Spotify has a daily global chart (link) that shows the top 200 songs on the platform for a certain day. There is a download button to save the data as a CSV file on each page. I would like to automatically download the CSV file somehow through code. I have tried to follow many web scraping tutorials, but nothing has worked so far.

My end goal would be to download every date on the global chart so far (Since Jan 2017), but I need to figure out how to download one date's CSV file first.

Any help would be greatly appreciated!

I've tried following multiple StackOverflow threads and online tutorials and none of them worked for this purpose.

1 Answer 1

2

To get those CSV files you need to be authenticated. Is this part of the plan?

You can, however, get the weekly charts direct from their open API without logging in.

import requests

response = requests.get('https://charts-spotify-com-service.spotify.com/public/v0/charts')

data = response.json()

for entry in response.json()["chartEntryViewResponses"][0]["entries"]:
    meta = entry["trackMetadata"]
    entry = entry["chartEntryData"]

    track = meta['trackName']
    artists = ", ".join([artist["name"] for artist in meta["artists"]])

    print(f"{entry['currentRank']:3} | {track:50} | {artists}")

Results look like this:

 1 | Espresso                                           | Sabrina Carpenter
  2 | Please Please Please                               | Sabrina Carpenter
  3 | BIRDS OF A FEATHER                                 | Billie Eilish
  4 | Houdini                                            | Eminem
  5 | MILLION DOLLAR BABY                                | Tommy Richman
  6 | Gata Only                                          | FloyyMenor, Cris Mj
  7 | LUNCH                                              | Billie Eilish
  8 | Too Sweet                                          | Hozier
  9 | I Had Some Help (Feat. Morgan Wallen)              | Post Malone, Morgan Wallen
 10 | A Bar Song (Tipsy)                                 | Shaboozey

You can do something similar with the daily charts, but that requires you to login and get an authentication token. Store that token in an environment variable, SPOTIFY_BEARER.

import os
import requests

AUTHORIZATION = os.getenv("SPOTIFY_BEARER")

DATE = "2024-01-01"

headers = {
    'authorization': f'Bearer {AUTHORIZATION}',
}

response = requests.get(
    f'https://charts-spotify-com-service.spotify.com/auth/v0/charts/regional-global-daily/{DATE}',
    headers=headers,
)

for entry in response.json()["entries"]:
    meta = entry["trackMetadata"]
    entry = entry["chartEntryData"]

    track = meta['trackName']
    artists = ", ".join([artist["name"] for artist in meta["artists"]])

    print(f"{entry['currentRank']:3} | {track:50} | {artists}")

That gives you the chart for a specific day (specified via the DATE variable). For example, on 1 January 2024 the top 10 songs were:

  1 | La Diabla                                          | Xavi                                                                                                                                                                         
  2 | greedy                                             | Tate McRae                                                                                                                                                                   
  3 | Cruel Summer                                       | Taylor Swift                                                                                                                                                                 
  4 | Lovin On Me                                        | Jack Harlow                                                                                                                                                                  
  5 | My Love Mine All Mine                              | Mitski                                                                                                                                                                       
  6 | La Víctima                                         | Xavi                                                                                                                                                                         
  7 | Seven (feat. Latto) (Explicit Ver.)                | Jung Kook, Latto                                                                                                                                                             
  8 | One Of The Girls (with JENNIE, Lily Rose Depp)     | The Weeknd, JENNIE, Lily-Rose Depp                                                                                                                                           
  9 | BELLAKEO                                           | Peso Pluma, Anitta                                                                                                                                                           
 10 | Paint The Town Red                                 | Doja Cat

Options for getting a token:

  1. Automate login to Spotify using something like Splash, Selenium or Playwright.
  2. Login to Spotify via your browser and just get a token directly. Not sure how long this will be valid though.

Not the answer you're looking for? Browse other questions tagged or ask your own question.