获取外链

http://music.163.com/song/media/outer/url?id=165237.mp3

只需要将上述地址的id改为音乐id即为外链地址

使用axios爬取歌单

具体思路: 先get到歌单页面地址,然后使用正则匹配歌单的DOM元素提取歌曲id与名字,批量转化为外链地址

以下代码即为示例:

import axios from 'axios'

// 歌单地址:https://music.163.com/playlist?id=2128245641
axios.get('https://music.163.com/playlist?id=2128245641')
    .then(r => {
        let [list, music] = [[], []]
        // 筛选出含有歌曲id的DOM结构
        list = r.data.match(/<a href="\/song\?id=[0-9].*?<\/a>/g)
        // 若歌单为空则退出
        if(!list){
            console.log('no music')
            return
        }
        // 去掉标签,保留id与歌名
        list = list.reduce((r, c) => [
            ...r,
            c.replace(/<a href="\/song\?id=|<\/a>/g, '').replace(/">/, ',')
        ], [])
        // 格式化list内容,转化为含有歌名,歌曲id,外链的对象数组
        music = list.reduce((r, c) => [
            ...r,
            {
                name: c.substring(c.indexOf(',') + 1, c.length),
                id: c.substring(0, c.indexOf(',')),
                src: `http://music.163.com/song/media/outer/url?id=${c.substring(0, c.indexOf(','))}.mp3`
            }
        ], [])
        console.log(music)
    })

// music
// [{
//     name: '丫头',
//     id: '165237',
//     src: 'http://music.163.com/song/media/outer/url?id=165237.mp3'
// },
// {
//     name: '离开你的城市后',
//     id: '863492743',
//     src: 'http://music.163.com/song/media/outer/url?id=863492743.mp3'
// }]

获取详细信息

歌曲的图片,演唱者,专辑,发行公司等都可以通过获取详细信息接口来获取:

http://music.163.com/api/song/detail/?id=[165237]&ids=[165237]&csrf_token=

将上述地址的id与ids中的歌曲id修改后请求结果示例:

{
    "songs": [
        {
            "name": "丫头",
            "id": 165237,
            "position": 1,
            "alias": [],
            "status": 0,
            "fee": 8,
            "copyrightId": 0,
            "disc": "1",
            "no": 1,
            "artists": [
                {
                    "name": "王童语",
                    "id": 5531,
                    "picId": 0,
                    "img1v1Id": 0,
                    "briefDesc": "",
                    "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "alias": [],
                    "trans": "",
                    "musicSize": 0,
                    "topicPerson": 0
                }
            ],
            "album": {
                "name": "回了又去 去了再回",
                "id": 16590,
                "type": "EP/Single",
                "size": 3,
                "picId": 803742999937768,
                "blurPicUrl": "http://p2.music.126.net/ZYkfNipPotMRfMo0a6pWWQ==/803742999937768.jpg",
                "companyId": 0,
                "pic": 803742999937768,
                "picUrl": "http://p2.music.126.net/ZYkfNipPotMRfMo0a6pWWQ==/803742999937768.jpg",
                "publishTime": 1163606400000,
                "description": "",
                "tags": "",
                "company": "古铜色文化",
                "briefDesc": "",
                "artist": {
                    "name": "",
                    "id": 0,
                    "picId": 0,
                    "img1v1Id": 0,
                    "briefDesc": "",
                    "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "alias": [],
                    "trans": "",
                    "musicSize": 0,
                    "topicPerson": 0
                },
                "songs": [],
                "alias": [],
                "status": 1,
                "copyrightId": 0,
                "commentThreadId": "R_AL_3_16590",
                "artists": [
                    {
                        "name": "王童语",
                        "id": 5531,
                        "picId": 0,
                        "img1v1Id": 0,
                        "briefDesc": "",
                        "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "albumSize": 0,
                        "alias": [],
                        "trans": "",
                        "musicSize": 0,
                        "topicPerson": 0
                    }
                ],
                "subType": null,
                "transName": null,
                "mark": 0
            },
            "starred": false,
            "popularity": 100.0,
            "score": 100,
            "starredNum": 0,
            "duration": 340297,
            "playedNum": 0,
            "dayPlays": 0,
            "hearTime": 0,
            "ringtone": "",
            "crbt": null,
            "audition": null,
            "copyFrom": "",
            "commentThreadId": "R_SO_4_165237",
            "rtUrl": null,
            "ftype": 0,
            "rtUrls": [],
            "copyright": 2,
            "transName": null,
            "sign": null,
            "mark": 0,
            "hMusic": {
                "name": "丫头",
                "id": 33195950,
                "size": 13626833,
                "extension": "mp3",
                "sr": 44100,
                "dfsId": 0,
                "bitrate": 320000,
                "playTime": 340297,
                "volumeDelta": -0.86
            },
            "mMusic": {
                "name": "丫头",
                "id": 33195951,
                "size": 6825593,
                "extension": "mp3",
                "sr": 44100,
                "dfsId": 0,
                "bitrate": 160000,
                "playTime": 340297,
                "volumeDelta": -0.42
            },
            "lMusic": {
                "name": "丫头",
                "id": 33195952,
                "size": 4105097,
                "extension": "mp3",
                "sr": 44100,
                "dfsId": 0,
                "bitrate": 96000,
                "playTime": 340297,
                "volumeDelta": -0.42
            },
            "bMusic": {
                "name": "丫头",
                "id": 33195952,
                "size": 4105097,
                "extension": "mp3",
                "sr": 44100,
                "dfsId": 0,
                "bitrate": 96000,
                "playTime": 340297,
                "volumeDelta": -0.42
            },
            "rtype": 0,
            "rurl": null,
            "mvid": 0,
            "mp3Url": null
        }
    ],
    "equalizers": {},
    "code": 200
}

相关公用接口

https://api.imjad.cn

Last modification:October 28, 2019
If you think my article is useful to you, please feel free to appreciate