# Goal
取得 Spotify 的
播放清單或專輯的基本曲目資訊。


# Info
| Key | Value |
|---|---|
| Date created | 2024/10/14 |
| Date modified | 2024/10/14 |
| Author | Benjamin Yang |
| Language | Python |
| Used module | re |
# Code
為了節省空間,請從以下 gist 取得範例代碼: https://gist.github.com/Benjamin-Yan/411e226890ac79f9584728df2186afbf
| 可選擇的模式 | 可取得的資料 | |
|---|---|---|
| 播放清單 | 播放清單模式 | 曲名 、 演出者 、 專輯 、 圖片網址 |
| 專輯模式 | 曲名 、 演出者 | |
| 專輯 | 專輯模式 | 曲名 、 演出者 |
import re | |
resp = '''{貼上範例於此} | |
''' | |
pattern0 = r'<div class=\"encore-text encore-text-body-medium encore-internal-color-text-base[^>]*>(.*?)<\/div>' | |
pattern1 = r'<a draggable="true" dir="auto"[^>]*>(.*?)<\/a>' | |
pattern2 = r'<a draggable=\"true\" class=\"standalone-ellipsis-one-line\"[^>]*>(.*?)<\/a>' | |
pattern3 = r'src="([^"]*)"' | |
isPlayList = int(input("Playlist mode: 1, album mode: 2\n")) | |
isTest = input("Input Spotify's html code (0 for default):\n") | |
if isTest != '0': | |
resp = isTest | |
print("\n------------------\n") | |
if isPlayList == 1: | |
# 0.Find title | |
title = re.findall(pattern0, resp) | |
# 1.Find artist | |
artist = re.findall(pattern1, resp) | |
# 2.Find 專輯 | |
album = re.findall(pattern2, resp) | |
# 3.Find img url | |
img = re.findall(pattern3, resp) | |
# Print | |
print(" ", "曲名", "演出者", "專輯", "圖片網址") | |
for i in range(len(title)): | |
print(i+1, title[i], artist[i], album[i], img[i]) | |
elif isPlayList == 2: | |
# 0.Find title | |
title = re.findall(pattern0, resp) | |
# 1.Find artist | |
artist = re.findall(pattern1, resp) | |
# Print | |
print(" ", "曲名", "演出者") | |
for i in range(len(title)): | |
print(i+1, title[i], artist[i]) | |
else: | |
print("Input mode error!") |
# 使用步驟
- 選擇模式。
- 參考範例 html 代碼,把該播放清單或專輯的相應代碼貼上,或使用範例代碼做測試。
# Reference
- 官方 範例播放清單