/ 批量下载

批量下载百度音乐

闲来无事搞的一个玩意,应该不涉及版权问题吧……

没兴趣看探究过程的同学直接拉到文章最下方红字处。

百度音乐提供了下载功能,但是却不能批量下载,要一个个手点,要是列表里有几百首音乐要下载,那不得累死,于是想搞个自动化的脚本。

一开始的想法是模拟用户登录,然后抓取下载地址。

这种方案感觉比较麻烦,要记录cookie之类的,然后还要从页面里提取信息,所以放弃。

后来查看了下网页源代码,发现页面里面是把音乐的id列表用json格式存放,然后又抓了下音乐的下载地址,很简单的字符串拼接:

http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=音乐id&rate=码率

根据id获取歌曲名:

post http://yinyueyun.baidu.com/data/cloud/songinfo?songIds=id1,id2,id3……

思路很简单:

获取音乐列表的json之后,一个json_decode转换为obj,然后foreach一下拼接字符串,最后输出即可。

但是这样会有个问题,需要用户查看网页的源代码,手动复制json字符串,这样操作对于一些小白用户来说有点困难,所以还需要更简便的方案。

于是我想到用js代码直接操作,最终用户只需要将js代码的拖到书签栏,在百度音乐页面点击一下这个书签就可以了。

因为百度音乐页面引入了jquery库,所以写起代码来可以省不少事

//默认码率 var rate = '320'; //弹出遮罩层 $('body').append('

'); //弹出层 $('body').append('
超高品质 320kbps  高 品 质 192kbps  标准品质 128kbps
转为下载地址  关闭
'); //选择码率 $('input[type=radio]').click(function(){ rate=this.value; }); //将已选择的歌曲输出到textarea中 function transfer(){ var len = $('.ui-reelList-checked').length; var songlist = []; var str = ''; for(var i=0;i<len;i++){ songlist.push(mbox.favorIds.data.songList[ $('.ui-reelList-checked:eq('+i+')').attr('reellist-row')]); } for(var x in songlist){ str += 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds='+songlist[x].id+'&rate='+rate+'#'+$('.ui-reelList-checked:eq('+x+') div:eq(0)').text()+'\n'; } $('#re').text(str); }

压缩成一行之后变成

function transfer(){for(var a=$(".ui-reelList-checked").length,b=[],c="",d=0;a>d;d++)b.push(mbox.favorIds.data.songList[$(".ui-reelList-checked:eq("+d+")").attr("reellist-row")]);for(var e in b)c+="http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds="+b[e].id+"&rate="+rate+"#"+$(".ui-reelList-checked:eq("+e+") div:eq(0)").text()+"\n";$("#re").text(c)}var rate="320";$("body").append('

超高品质 320kbps  高 品 质 192kbps  标准品质 128kbps
转为下载地址  关闭
'),$("input[type=radio]").click(function(){rate=this.value});

然后就是写成一个a标签,并放置在一个单独的html页面中。

批量下载

但是访问的时候发现页面并不只显示一个a标签,还有遮罩层都显示出来了,应该是单引号和双引号需要转义的问题。

转义之后问题依旧……难道是a标签里面的href属性不支持转义?有知道的同学告知一声。

于是只能另作打算,以前玩过网马的加密解密,这就用到了,把那个字符串escape加密一下单引号和双引号就被编码了,变成这样的

eval(unescape("function%20transfer%28%29%7Bfor%28var%20a%3D%24%28%22.ui-reelList-checked%22%29.length%2Cb%3D%5B%5D%2Cc%3D%22%22%2Cd%3D0%3Ba%3Ed%3Bd++%29b.push%28mbox.favorIds.data.songList%5B%24%28%22.ui-reelList-checked%3Aeq%28%22+d+%22%29%22%29.attr%28%22reellist-row%22%29%5D%29%3Bfor%28var%20e%20in%20b%29c+%3D%22http%3A//yinyueyun.baidu.com/data/cloud/downloadsongfile%3FsongIds%3D%22+b%5Be%5D.id+%22%26rate%3D%22+rate+%22%23%22+%24%28%22.ui-reelList-checked%3Aeq%28%22+e+%22%29%20div%3Aeq%280%29%22%29.text%28%29+%22%5Cn%22%3B%24%28%22%23re%22%29.text%28c%29%7Dvar%20rate%3D%22320%22%3B%24%28%22body%22%29.append%28%27%3Cdiv%20id%3D%22fade%22%20style%3D%22display%3Ablock%3Bbackground%3A%23000%3Bposition%3Afixed%3Bleft%3A0%3Btop%3A0%3Bwidth%3A100%25%3Bheight%3A100%25%3Bopacity%3A.80%3Bz-index%3A9999%3B%22%3E%3C/div%3E%3Cdiv%20id%3D%22result%22%20style%3D%22background%3A%23fff%3Bpadding%3A20px%3Bborder%3A20px%20solid%20%23ddd%3Bfloat%3Aleft%3Bposition%3Afixed%3Btop%3A30%25%3Bleft%3A25%25%3Bz-index%3A99999%3B%22%3E%3Cinput%20type%3D%22radio%22%20name%3D%22rate%22%20value%3D%22320%22%20checked%3D%22true%22%3E%u8D85%u9AD8%u54C1%u8D28%20320kbps%26nbsp%3B%26nbsp%3B%3Cinput%20type%3D%22radio%22%20name%3D%22rate%22%20value%3D%22192%22%3E%u9AD8%20%u54C1%20%u8D28%20192kbps%26nbsp%3B%26nbsp%3B%3Cinput%20type%3D%22radio%22%20name%3D%22rate%22%20value%3D%22128%22%3E%u6807%u51C6%u54C1%u8D28%20128kbps%3Cbr%3E%3Ca%20href%3D%22javascript%3Atransfer%28%29%22%3E%u8F6C%u4E3A%u4E0B%u8F7D%u5730%u5740%3C/a%3E%26nbsp%3B%26nbsp%3B%3Ca%20href%3D%22javascript%3A%24%28%5C%27%23fade%5C%27%29.remove%28%29%3B%24%28%5C%27%23result%5C%27%29.remove%28%29%3B%22%3E%u5173%u95ED%3C/a%3E%3Cbr%3E%3Ctextarea%20id%3D%22re%22%20rows%3D%2210%22%20cols%3D%22120%22%3E%3C/textarea%3E%3C/div%3E%27%29%2C%24%28%22input%5Btype%3Dradio%5D%22%29.click%28function%28%29%7Brate%3Dthis.value%7D%29%3B"));

将这串代码在控制台中运行,能够达到预期效果。

于是再一次将这个代码写入a标签,这回正常了,将这个a标签拖入书签栏,切换到百度音乐页面,点击这个书签,却发现没有效果,控制台也报错了。

右键点击这个书签,选择修改,将书签的内容复制出来,和我之前写到a标签的值对比了下,发现不太一样,%20和%22被转回成空格和双引号,不报错才怪,于是再escape一次,终于可以使用了。

批量下载

将下方的“批量下载”拖至书签栏,然后在百度音乐的页面勾选要下载的音乐,再点击这个书签即可。

支持http://yinyueyun.baidu.com/ 和 http://play.baidu.com/ 这个两个站点。

→→ [批量下载](javascript:eval%28unescape%28%22function%2520transfer%2528%2529%257Bfor%2528var%2520a%253D%2524%2528%2522.ui-reelList-checked%2522%2529.length%252Cb%253D%255B%255D%252Cc%253D%2522%2522%252Cd%253D0%253Ba%253Ed%253Bd++%2529b.push%2528mbox.favorIds.data.songList%255B%2524%2528%2522.ui-reelList-checked%253Aeq%2528%2522+d+%2522%2529%2522%2529.attr%2528%2522reellist-row%2522%2529%255D%2529%253Bfor%2528var%2520e%2520in%2520b%2529c+%253D%2522http%253A//yinyueyun.baidu.com/data/cloud/downloadsongfile%253FsongIds%253D%2522+b%255Be%255D.id+%2522%2526rate%253D%2522+rate+%2522%2523%2522+%2524%2528%2522.ui-reelList-checked%253Aeq%2528%2522+e+%2522%2529%2520div%253Aeq%25280%2529%2522%2529.text%2528%2529+%2522%255Cn%2522%253B%2524%2528%2522%2523re%2522%2529.text%2528c%2529%257Dvar%2520rate%253D%2522320%2522%253B%2524%2528%2522body%2522%2529.append%2528%2527%253Cdiv%2520id%253D%2522fade%2522%2520style%253D%2522display%253Ablock%253Bbackground%253A%2523000%253Bposition%253Afixed%253Bleft%253A0%253Btop%253A0%253Bwidth%253A100%2525%253Bheight%253A100%2525%253Bopacity%253A.80%253Bz-index%253A9999%253B%2522%253E%253C/div%253E%253Cdiv%2520id%253D%2522result%2522%2520style%253D%2522background%253A%2523fff%253Bpadding%253A20px%253Bborder%253A20px%2520solid%2520%2523ddd%253Bfloat%253Aleft%253Bposition%253Afixed%253Btop%253A30%2525%253Bleft%253A25%2525%253Bz-index%253A99999%253B%2522%253E%253Cinput%2520type%253D%2522radio%2522%2520name%253D%2522rate%2522%2520value%253D%2522320%2522%2520checked%253D%2522true%2522%253E%25u8D85%25u9AD8%25u54C1%25u8D28%2520320kbps%2526nbsp%253B%2526nbsp%253B%253Cinput%2520type%253D%2522radio%2522%2520name%253D%2522rate%2522%2520value%253D%2522192%2522%253E%25u9AD8%2520%25u54C1%2520%25u8D28%2520192kbps%2526nbsp%253B%2526nbsp%253B%253Cinput%2520type%253D%2522radio%2522%2520name%253D%2522rate%2522%2520value%253D%2522128%2522%253E%25u6807%25u51C6%25u54C1%25u8D28%2520128kbps%253Cbr%253E%253Ca%2520href%253D%2522javascript%253Atransfer%2528%2529%2522%253E%25u8F6C%25u4E3A%25u4E0B%25u8F7D%25u5730%25u5740%253C/a%253E%2526nbsp%253B%2526nbsp%253B%253Ca%2520href%253D%2522javascript%253A%2524%2528%255C%2527%2523fade%255C%2527%2529.remove%2528%2529%253B%2524%2528%255C%2527%2523result%255C%2527%2529.remove%2528%2529%253B%2522%253E%25u5173%25u95ED%253C/a%253E%253Cbr%253E%253Ctextarea%2520id%253D%2522re%2522%2520rows%253D%252210%2522%2520cols%253D%2522120%2522%253E%253C/textarea%253E%253C/div%253E%2527%2529%252C%2524%2528%2522input%255Btype%253Dradio%255D%2522%2529.click%2528function%2528%2529%257Brate%253Dthis.value%257D%2529%253B%22%29%29%3B) ←←

批量下载百度音乐
Share this