
Spotify streams over 400 million tracks daily. Imagine unlocking that data goldmine—track names, artists, durations—all at your fingertips for analysis or app building. You don't have to be a data wizard to make it happen. Python makes it straightforward.
Spotify offers a robust API that's both legal and efficient for pulling playlist data. But when the API doesn't cut it, web scraping steps in. Tools like BeautifulSoup and Selenium become your best friends. They help you extract exactly what you need, even from dynamic pages that load content on the fly.
In this guide, we'll show you how to use Selenium and BeautifulSoup to scrape Spotify playlists and save your data neatly for analysis or further use.
First, grab the tools that make scraping work:
pip install beautifulsoup4 selenium requests
Why these?
BeautifulSoup parses static HTML like a champ, letting you pull out track info from simple pages.
Selenium handles the heavy lifting on dynamic pages—clicking buttons, scrolling, loading content automatically.
Requests handles your API calls and simple HTTP requests. Lightweight and efficient.
Selenium needs a web driver to control your browser. We recommend ChromeDriver for Chrome users.
Download ChromeDriver from the official site.
Extract it and note the path.
Here's how to launch a browser window with Selenium:
from selenium import webdriver
driver_path = "C:/webdriver/chromedriver.exe"  # Update with your actual path
driver = webdriver.Chrome(driver_path)
driver.get("https://google.com")  # Just a test page
Open Spotify, press F12, and inspect the page. You’ll find track details wrapped in specific HTML classes. That's your target.
Here's a Python function that loads a Spotify playlist, scrolls to load all tracks, then pulls the track name, artist, and duration:
from selenium import webdriver
from bs4 import BeautifulSoup
import time
def get_spotify_playlist_data(playlist_url):
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")  # No browser window
    driver = webdriver.Chrome(options=options)
    
    driver.get(playlist_url)
    time.sleep(5)  # Let the page load
    # Scroll to bottom to load all tracks
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(3)  # Wait for dynamic content
    
    html = driver.page_source
    driver.quit()
    
    soup = BeautifulSoup(html, "lxml")
    tracks = []
    
    # Update class selectors based on actual Spotify HTML structure
    for track in soup.find_all(class_="tracklist-row"):
        name = track.find(class_="track-name").text.strip()
        artist = track.find(class_="artist-name").text.strip()
        duration = track.find(class_="track-duration").text.strip()
        tracks.append({"track title": name, "artist": artist, "duration": duration})
    
    return tracks
Pass your playlist URL to this function. Boom—you’ve got a list of songs with all the details.
Scraping can be fragile and against terms if done irresponsibly. The Spotify API is safer and gives you structured data directly.
Here's how to get your access token:
Register your app at Spotify Developer Dashboard.
Get your Client ID and Client Secret.
Run this Python snippet:
import requests
import base64
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
credentials = f"{CLIENT_ID}:{CLIENT_SECRET}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
url = "https://accounts.spotify.com/api/token"
headers = {
    "Authorization": f"Basic {encoded_credentials}",
    "Content-Type": "application/x-www-form-urlencoded"
}
data = {"grant_type": "client_credentials"}
response = requests.post(url, headers=headers, data=data)
token = response.json().get("access_token")
print("Access Token:", token)
With this token, you can request artist info, playlists, and more—no scraping necessary.
Export your gathered data as JSON or CSV. Easy for later analysis.
import json
data = get_spotify_playlist_data("your_playlist_url")
with open('tracks.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)
    print("Data saved to tracks.json")
Always use Spotify API when possible. It's legal and reliable.
When scraping, respect rate limits and robots.txt to avoid getting blocked.
Use proxies if scraping large volumes to distribute requests.
Keep your code ready for changes—Spotify updates its site frequently.
Whether you use the API or scraping, Python gives you powerful tools to unlock Spotify's data treasure chest. Dive in, experiment, and create something amazing.
 頂級住宅代理解決方案
頂級住宅代理解決方案 {{item.title}}
                                        {{item.title}}