Post

批量下载音乐的py脚本

过年了,想着把我车上U盘的音乐更新下, 网上找了半天没有找到什么好的下载地方。

最后发现一个不错的网站-煎饼网,https://www.jbsou.cn/

这个网站可以下载mp3, 很良心了,然后我看到它居然带了一个播放器,而且有歌单,这个就很重要了。

于是花了3个消失,写了一段脚本。 直接下载,挺好的。

这里把代码贴出来:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 煎饼网 爬取mp3

# songSheetList 变量导出歌单,然后使用脚本爬取
# https://www.jbsou.cn/
import requests
import json
import os
import time
savePath =  "C:/Users/Miste/Desktop/test2/"

with open("./mp3.json", "r", encoding="utf-8") as file :
    content = file.read()
    data = json.loads(content)

    for j in range(len(data)):
        dirname = data[j]["songSheetName"] + "_"  + data[j]["author"] 
        tmpdir = savePath + "/" + dirname
        if(False == os.path.exists(tmpdir)):
            os.makedirs(tmpdir)
        print("start download :" + dirname)

    
        target = data[j] 
        for i in range(len(target["songIds"])):
            musicName = target["songNames"][i]
            musicId = target["songIds"][i]
            sign = target["sign"][i]
            musicName = musicName.replace("|", "") \
                .replace("\\", "") \
                .replace("/", "") \
                .replace("*", "") \
                    .replace("?", "") \
                    .replace("<", "") \
                    .replace(">", "") \
                    .replace(":", "") \
                    .replace('"', "")
            
            url  = 'https://myhkw.cn/api/url?song={0}&type=kg&id=163374283426&sign={1}'.format(musicId, sign)
            res = requests.get(url, headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"})
            if(res.status_code == 200):
                mp3pathname = tmpdir +  "/" + musicName + ".mp3"
                with open(mp3pathname, "wb") as file2 :
                    file2.write(res.content)                
                pass
            else:
                print("error->" + url)

            time.sleep(0.1)

    print("all data downloaded!!!")
This post is licensed under CC BY 4.0 by the author.