QQ音乐爬虫(NodeJs)
Posted warrenlee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QQ音乐爬虫(NodeJs)相关的知识,希望对你有一定的参考价值。
一、创建一个文件夹
例如我创建在D:codeqq-music
在文件目录打开cmd
在cmd中执行 npm init 项目初始化
然后安装一些工具包:
npm i request -s
npm i querystring -s
在目录下创建一个Music的文件夹用于存储下载的音乐文件
二、直接上代码 D:codeqq-musicqqMusic.js
const request = require(‘request‘);
const fs = require(‘fs‘); //文件模块
const querystring=require("querystring");
const downMusic = function(UrlJsonObj){
const baseUrl = "http://124.232.155.157/amobile.music.tc.qq.com/";
for(var attr in UrlJsonObj){
const musicJsonObj = UrlJsonObj[attr];
const songname = musicJsonObj.songname;
const songUrl = musicJsonObj.songUrl;
request(baseUrl+songUrl).pipe(fs.createWriteStream(‘./Music/‘+songname+‘.m4a‘))
}
};
const makeMusicJsonArray = function(songJsonArray){
const baseUrl = "https://u.y.qq.com/cgi-bin/musicu.fcg";
let UrlJsonObj = {};
songJsonArray.map(function(musicInfo){
const songname = musicInfo.songname;
const songmid = musicInfo.songmid;
const url = baseUrl+"?"+querystring.stringify(musicInfo.params);
request(url, function (error, response, body) {
const BodyJson = JSON.parse(body);
let musicInfo = {
songname : this.songname
};
musicInfo["songUrl"] = BodyJson["req_0"]["data"]["midurlinfo"][0]["purl"];
UrlJsonObj[this.songmid] = musicInfo;
if(Object.getOwnPropertyNames(UrlJsonObj).length == songJsonArray.length){
this.downMusic(UrlJsonObj)
}
}.bind({
songname : songname,
songmid : songmid,
downMusic : downMusic
}));
});
};
下面标红的是我在这个页面找到的歌曲清单接口
const queryMusicList = function(){
request(‘https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2018_41&topid=26&type=top&song_begin=0&song_num=300&g_tk=5381&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0‘, function (error, response, body) {
if(response.statusCode == 200){
body = JSON.parse(body);
const songList = body.songlist;
let songJsonArray = [];
songList.map(function(songDetail){
songDetail = songDetail.data;
const songJsonObj = {
‘albumname‘ : songDetail.albumname,
‘albummid‘ : songDetail.albummid,
‘singer‘: songDetail.singer[0]["name"],
‘songname‘: songDetail.songname,
‘songmid‘: songDetail.songmid,
‘songtype‘: songDetail.songtype
};
const musicParams = {
"req":{
"module":"CDN.SrfCdnDispatchServer",
"method":"GetCdnDispatch",
"param":{
"guid":"1111",
"calltype":0,
"userip":""
}
},
"req_0":{
"module":"vkey.GetVkeyServer",
"method":"CgiGetVkey",
"param":{
"guid":"1111",
"songmid":[songDetail.songmid],
"songtype":[songDetail.songtype],
"uin":"0",
"loginflag":1,
"platform":"20"
}
},
"comm":{
"uin":0,
"format":"json",
"ct":20,
"cv":0
}
};
var params = {
loginUin : 0,
hostUin : 0,
format : ‘jsonp‘,
inCharset: ‘utf8‘,
outCharset: ‘utf-8‘,
notice: 0,
platform: ‘yqq‘,
needNewCode: 0,
data: encodeURI(JSON.stringify(musicParams))
};
songJsonObj["params"] = params
songJsonArray.push(songJsonObj)
});
makeMusicJsonArray(songJsonArray);
}
});
};
queryMusicList();
三、运行代码
node qqMusic.js
PS:纯粹是娱乐一下,打发休息时间
以上是关于QQ音乐爬虫(NodeJs)的主要内容,如果未能解决你的问题,请参考以下文章