How to Scrape and Download Songs from Kugou Music with Python
This tutorial walks you through using Python's requests library to inspect Kugou Music's web requests, simulate them, extract MP3 URLs, and download songs, covering preparation, goal setting, implementation steps, and a concise project summary.
Preface
Many music apps require payment or subscription even after downloading, which can be frustrating for users who prefer free solutions. This guide explores how to bypass those restrictions by programmatically downloading songs from Kugou Music.
Project Preparation
Editor: Sublime Text 3
Browser: 360 Browser
Project Goal
Download the desired music tracks directly from Kugou.
Project Implementation
1. Open the Kugou Music homepage in 360 Browser.
2. Open the browser's developer tools, go to the Network tab, and observe the request parameters used for searching songs.
From the captured request we see that the searchKeyWord parameter holds the song name, while other parameters can remain default.
3. Simulate the request using Python's requests library.
import requests
headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cookie": "kg_mid=...; UM_distinctid=...; ...",
"referer": "https://www.kugou.com/yy/html/search.html",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
}
song_name = input('请输入歌名:')
data = {
"callback": "jQuery112408716317197794392_1590368232677",
"keyword": song_name,
"page": "1",
"pagesize": "30",
"userid": "-1",
"clientver": "",
"platform": "WebFilter",
"tag": "em",
"filter": "2",
"iscorrection": "1",
"privilege_filter": "0",
"_": "1590368232679"
}
res = requests.get('https://www.kugou.com/yy/html/search.html', params=data, timeout=4)
print(res)Running the script prints the response object, confirming that the request was successfully simulated.
4. Retrieve the list of music files by following the redirected URL.
rep = requests.get('https://www.kugou.com/yy/html/search.html', params=data, timeout=5)
print(rep.url)
res = requests.get(rep.url, timeout=4)
print(res.text)The returned content is not a clean JSON; it contains extra characters that need to be cleaned before extracting the MP3 URLs.
5. Download the audio file. After performing a search, open the Network tab again, locate the request that returns a JSON payload containing the MP3 URL, extract the URL, and download the file.
By extracting the clean MP3 link from the JSON, the song can be downloaded directly.
Project Summary
Unlike QQ Music, Kugou Music's download links are easier to capture; they can be obtained directly from the playback page. Simulating the request and extracting the MP3 URL completes the download process.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
