啦啦啦,之前一直在做个音乐网站,然后涉及到了歌词,可当时自己不懂歌词的爬取链接,所以一直采用的手录歌词的方法,费时又费力。前两天在一篇CSDN文章上发现了网易云的歌词链接。然后就立马利用起来爬取一拨歌词...链接如下所示
http://music.163.com/api/song/lyric?id="+id+"&lv=1&kv=1&tv=-1
链接里面其实返回的是包含歌词的json信息,如果要得到纯歌词,还要对其进行一番提取。目前可以操作json的jar包也还挺多的。不过嘛,我也没心思去学习使用,所以一般都是自写方法去提炼需要的信息,虽然麻烦点,但也还好啦,能得到想要的结果。
如下是爬取歌词的代码
1.首先定义二维数组,包含所有要下载歌词的歌曲名以及在网易云音乐上的id
private static String song[][]={
{"25643328","信仰-张信哲"},{"254574","后来-刘若英"},{"174934","无颜女-徐良,小凌"},{"174962","和平分手-徐良"},{"26609879","情话-徐良,孙羽幽"},
{"174960","犯贱-徐良"},{"174961","红装-徐良"},{"25918133","那时雨-徐良"},{"27890395","抽离-徐良,刘丹萌"},{"174963","客官不可以-徐良,小凌"},
{"26609877","七秒钟的记忆-徐良,孙羽幽"},{"375381","不想长大-S.H.E"},{"354620","只对你有感觉-飞轮海 ,田馥甄"},{"167815","情侣装-许嵩"},{"394748","棉花糖-至上励合"},
{"122535","最后一次的温柔"},{"28853662","秋殇别恋"},{"452986458","红昭愿"},{"486111543","7538(Me U-Remix)"},{"515143305","9420"},
{"31134193","出卖"},{"31654343","不将就"},{"29764564","自拍"},{"27731177","太坦白"},{"27731176","模特"},
{"29764562","作曲家"},{"133998","老街"},{"27678655","李白"},{"149787","我不是没脸的男孩"},{"25864481","微情歌"},
{"149751","那些学校没有教过的事儿"},{"25640407","一千个分手的理由"},{"149778","最简单的声音"},{"149789","致命的甜蜜"},{"149763","毕业后你不是我的"},
{"167888","认错"},{"167873","多余的解释"},{"167827","素颜"},{"167876","有何不可"},{"553755659","可不可以-张紫豪"},
{"175072","不再联系-夏天Alex"},{"28661853","我配不上你-夏天Alex"},{"69827","太多-陈冠蒲"},{"156193","为你写诗"},{"1330348068","起风了"},
{"77131","有多少爱可以重来-迪克牛仔"},{"26599373","当我孤独时还可以抱你-郑源"},{"27808044","丑八怪-薛之谦"},{"169185","认真的雪-薛之谦"},{"415792881","刚刚好-薛之谦"},
{"27955654","其实-薛之谦"},{"32192436","绅士-薛之谦"},{"32507038","演员"},{"163123","秋天不回来-王强"},{"5256103","死了都要爱"},
{"19081573","Util You"},{"2006865","Only Love"},{"19081569","Breathless"},{"21157332","One Day"},{"472361096","Despacito (Remix)"},
{"25795016","有没有人告诉你-陈楚生"},{"165367","三国杀-汪苏泷"},{"36990266","Faded-Alan Walker&Iselin Solheim"},{"150361","三国恋-Tank"},{"77437","2002年的第一场雪-刀郎"},
{"557584888","往后余生-马良"},{"486814412","沙漠骆驼"},{"29019227","sugar"},{"1210461","what are words"},{"28832241","superheroes"},
{"4875306","逍遥叹"},{"4872532","爱的华尔兹"},{"4872530","让我为你唱首歌"},{"413812448","大鱼"},{"110184","谢谢你的爱"},
{"178176","擦肩而过"},{"88926","想你的夜"},{"526464293","空空如也"},{"523251118","说散就散"},{"4172882","Fairy Tale"},
{"126443","窗外"},{"482999696","夏至未至"},{"483671599","追光者"},{"569200213","消愁"},{"569213220","像我这样的人"},
{"28481105","机器铃 砍菜刀-张卫"},{"28240119","倍儿爽-大张伟"},{"413829859","童话镇-暗杠"},{"95843","栀子花开-何炅"}
};
2.接下来写方法爬取
/**
* @desc 爬取歌词并下载
* @author zcl
* @date 2019年4月5日
* @param id 歌曲的id
* @param name 歌曲名
* @throws IOException
*/
public static void downloadLyric(String id,String name) throws IOException{
String lyricUrl="http://music.163.com/api/song/lyric?id="+id+"&lv=1&kv=1&tv=-1";
Document doc=Jsoup.connect(lyricUrl).get();
String lyric=doc.toString();
lyric=lyric.substring(lyric.indexOf("lyric\"")+8, lyric.indexOf("klyric")-4);//去掉其他json格式
lyric=lyric.replace("\\n", "\r");//\n替换为\r
writeToFile(lyric, name);
}
3.再接下来定义方法,将爬取到的歌词下载至本地文件中
/**
* 输出歌词至指定文件中
* @desc
* @author zcl
* @date 2019年4月5日
* @param lyric 爬取的歌词
* @param name 歌曲名即保存的文件名
* @throws IOException
*/
public static void writeToFile(String lyric,String name) throws IOException{
File file=new File("E:/AAAA/alyric/"+name+".txt");
file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
fos.write(lyric.getBytes());
System.out.println("已保存《"+name+"》的歌词至“"+file.getAbsolutePath()+"”文件");
}
4.最后,在main函数中执行循环来逐个下载歌词。
for(int i=0;i
本篇博文已同步至本人个人博客网站,欢迎访问
欢迎大家访问我的视频网站哆啦网,注册并登陆后即可拥有发表评论,记录自己的播放,搜索,登陆记录以及收藏视频等功能。我在553影院等你