NodeJS 获取酷狗音乐

先获得歌手列表,将歌手列表存贮到本地

var request = require('request');
var cheerio = require('cheerio');
var path = require('path'); 
var fs = require('fs');

var out = fs.createWriteStream('./test1.txt');

var listUrl=new Array(); 
var filenames=new Array();

var requrl="http://www.kugou.com/yy/singer/index/1-a-1.html";
var headurlbase= 'http://www.kugou.com/yy/singer/index/'
var singerList=new Array()
var page_Num=0;

var reqlist=[]

for(let i=97;i<123;i++){
	var headbasestring= String.fromCharCode(i)
	var headurl = headurlbase+'1-'+headbasestring+'-1.html'
	//console.log( 'headurl  : ' +headurl );
	for (let j=1;j<=5;j++){
		requrl = headurlbase+j+'-'+headbasestring+'-1.html'
		reqlist.push(requrl);
		//console.log('curent url is ' + requrl)
	}
	for(let k=2; k<12;k++){
		requrl = headurlbase+'1-'+headbasestring+'-'+k+'.html'
		reqlist.push(requrl);
		//console.log('curent url is ' + requrl)
	}
} 	
console.log('End....'+reqlist.length);
//for(let i=0; i< reqlist.length;i++){
for(let i=0; i< 390;i++){
	console.log(' i =  ['+i+']   current get the url :  ---'+reqlist[i])
	getHtmlPage(reqlist[i])
}



function getHtmlPage(url){
	request(url, function (error, response, body) {
		if (!error && response.statusCode == 200) {
			var tmplist =acquireSingerData(body)
			singerList.push.apply(singerList,tmplist)
		}
		console.log('singerList length ====='+singerList.length)
		console.log(singerList)


	});
}

function acquirePageNum(data){
	var $ = cheerio.load(data);
	console.log('start find page ');
	var pageNum=$('#mypage').find('a').eq(-3).text()   // 通过索引筛选匹配的元素。使用.eq(-i)就从最后一个元素向前数。
	console.log('mypage :'+pageNum)
	return pageNum
	
}



function acquireSingerData(data) { 
	var tmpsingerlist=[]

	var $ = cheerio.load(data);
	//网页内容装入cheerio中之后,查找网页节点元素, 加入'#'和加入’.‘查找的方式不一样
	
	//topsingerlist=$('#list_head strong').toArray();  //找到list_head节点下所有strong的节点
	
	//前18个歌手信息
	var topsingerlist=$('#list_head')
	console.log("singer info is :"+topsingerlist.length);
	topsingerlist.find('li').each(function(item){
		var  infos=$(this)
		let singername=infos.find('strong').find('a').text()
		let singerurl=infos.find('strong').find('a').attr('href')
		out.write(singerurl)
		out.write('\n')
		
		console.log('singer name :  ['+singername+']   地址 :'+singerurl)
		
		
		
		var singerData={
			singername:singername,
			singerurl:singerurl
		};
		tmpsingerlist.push(singerurl);
	});
	
	//后32个歌手信息
	var lastsingerlist=$('#list1 a').toArray();
	console.log("list1 :"+lastsingerlist.length);
	for(let i = 0; i < lastsingerlist.length;i++ ){
		let singername=lastsingerlist[i].attribs.title;
		
		let singerurl=lastsingerlist[i].attribs.href; 
		console.log('singer name :['+singername+']   地址 :'+singerurl)
		out.write(singerurl)
		out.write('\n')
		
		var singerData={
			singername:singername,
			singerurl:singerurl
		};
		tmpsingerlist.push(singerurl);
	}


	//console.log(tmpsingerlist)
	
	return tmpsingerlist
} 


	

根据歌手列表下载歌曲(初版代码)


var request = require('request');
var cheerio = require('cheerio');
var path = require('path'); 
var fs = require('fs');
var requrl="http://www.kugou.com/singer/420.html";  //陈奕迅歌单
var requrl="http://www.kugou.com/yy/singer/home/1574.html";  //林俊杰歌单
//var requrl="http://www.kugou.com/yy/singer/home/192923.html";     //摩登兄弟歌单
var listUrl=new Array(); 
var filenames=new Array();

var  songsFolderName

request(requrl, function (error, response, body) {
	if (!error && response.statusCode == 200) {
		acquireData(body); 
	}
	
	for(let i=0;i<listUrl.length;i++){
		let time=new Date().getTime();
		request('http://www.kugou.com/yy/index.php?r=play/getdata&hash='+listUrl[i]+'&album_id=0'+'&_='+time, function (error, response, body) { 
			if (!error && response.statusCode == 200) {
				acquireMusic(body,i); 
			} 
		}); 
	}
	
	
});

function acquireData(data) { 
	var $ = cheerio.load(data);
	songsFolderName  = $('.mbx').find('span').text();
	songsFolderName=songsFolderName+'/'
	console.log("singer info is :"+songsFolderName);
	
	fs.exists(songsFolderName,function(exists){
		if (exists){
			console.log("歌手目录["+songsFolderName+"]已经存在,无需创建");
		}
		else{
			fs.mkdir(songsFolderName,function(err){
				if (err) {
					return console.error(err);
				}
				console.log("歌手目录["+songsFolderName+"]创建成功");
			});
		}
	});
		
	var songlist = $('#song_container input').toArray(); 
	//console.log(songlist);
	
	for(let i=0;i<songlist.length;i++){ 
		let info=songlist[i].attribs.value; 
		//console.log("info=songlist[i].attribs.value:"+info);
		let reg=/\|/; 
		let hash=new Array(); 
		//hash=info.split(reg);
		hash=info.split('|');
		//console.log("info.split(reg):"+hash);
		listUrl.push(hash[1]); 
		//console.log("hash[1] : "+hash[1]); 
		//console.log("hash[0] : "+hash[0]); 
		filenames.push(hash[0]); 
	} 
	
} 

function acquireMusic(data,Num){
	var info=JSON.parse(data);
	var imgsrc=info.data.play_url; 
	var lyricInfo=info.data.lyrics;
	//var filename = parseUrlForFileName(imgsrc);
	var filename=filenames[Num]; 
	console.log("current num is "+Num+"   current song is:"+filename);
	/*
	//console.log("current num is "+Num+"current info.data.audio_name is:"+info.data.audio_name);
	//console.log("current num is "+Num+"current info.data.author_name is:"+info.data.author_name);
	
	for (let item in info ){
		var inter = info[item];
		console.log(inter);	
	}*/
	
	downloadLyric(lyricInfo,filename,function(){
		console.log("current num is["+Num+"]  ---- :["+filename+"]'s  lyric has donwload done");
	});
	
	downloadImg(imgsrc,filename,function(){ 
		console.log("current num is ["+Num+"] ----  :["+filename + '] 的歌曲写入本地已经完成 ');
	}); 
	
}

function parseUrlForFileName(address) { 
	var filename = path.basename(address); 
	return filename; 
}

var downloadLyric=function(lyricInfo,filename,callback) {
	out=fs.createWriteStream(songsFolderName+filename+'.lyc');
	out.write(lyricInfo);
	out.end("close",callback);
}


var downloadImg = function(uri, filename, callback){
	request.head(uri, function(err, res, body){ 
		if (err) { 
			console.log('err: '+ err); 
			return false; 
		} 
		//console.log('res: '+ res); 
		console.log('正在位置 : '+ songsFolderName+'写入文件  --------【'+filename+'.mp3】 它的下载地址为:----'+uri); 
		request(uri).pipe(fs.createWriteStream(songsFolderName+filename+'.mp3')).on('close', callback); //调用request的管道来下载到 images文件夹下 
	});
};
	

 

posted @ 2018-08-29 14:51  枫奇丶宛南  阅读(64)  评论(0编辑  收藏  举报