谁把年华错落成诗 - 爬取
type: channel
link: https://blog.pomears.com/tag/%E7%88%AC%E5%8F%96/
items: 纯前端爬取网易云音乐歌单音乐外链以及详细信息

纯前端爬取网易云音乐歌单音乐外链以及详细信息
type: item
date: 2019-07-09T18:20:00+08:00
description: 获取外链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/ZYkfNipPotMRf…