Skip to content

Instantly share code, notes, and snippets.

@markuskreitzer
Created August 17, 2021 17:27
Show Gist options
  • Save markuskreitzer/dd04c144b26815ac84a97139b2181d04 to your computer and use it in GitHub Desktop.
Save markuskreitzer/dd04c144b26815ac84a97139b2181d04 to your computer and use it in GitHub Desktop.
This program will use requests and beautiful soup to parse out the name and artist from a publicly visible Apple Music playlist. This is useful to take the data from this program to create playlists for Spotify, Youtube and others. Would love to find something that will take this information and generate a Youtube playlist that's easier to share…
#!/usr/bin/env python3
"""
Author: elec3647
This program will use requests and beautiful soup to parse out the name and artist from a publicly visible Apple Music playlist.
This is useful to take the data from this program to create playlists for Spotify, Youtube and others. Would love to find something that
will take this information and generate a Youtube playlist that's easier to share with folks that do not have a music streaming subscription.
"""
import re
import requests
from bs4 import BeautifulSoup
import sys
my_url = sys.argv[1]
headers = {
'authority': 'music.apple.com',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-gpc': '1',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'accept-language': 'en-US,en;q=0.9'
}
response = requests.get(my_url, headers=headers)
s = BeautifulSoup(response.content)
playlist = s.find('div', {'class': 'product-page playlist'})
songs = playlist.find_all('div', {'class': 'songs-list-row__song-name-wrapper'})
playlist = []
for song in songs:
song_name = song.find('div', {'class': 'songs-list-row__song-name'}).text
artists = song.find('div', {'class': 'songs-list-row__by-line'})
artist = re.sub('\s+', ' ', artists.text.strip())
print(song_name, '-' , artist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment