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.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Scrape and Download Songs from Kugou Music with Python

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialrequestsweb-scrapingkugoumusic download
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.