百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

Java爬取并下载酷狗TOP500歌曲

toyiye 2024-06-21 19:30 28 浏览 0 评论

是这样的,之前买车送的垃圾记录仪不能用了,这两天狠心买了好点的记录仪,带导航、音乐、蓝牙、4G等功能,寻思,既然有这些功能就利用起来,用4G听歌有点奢侈,就准备去酷狗下点歌听,居然都是需要办会员才能下载,而且vip一月只能下载300首,我这么穷又这么抠怎么可能冲会员,于是百度搜了下怎么免费下载,都是python爬取,虽然也会一点,但是电脑上没安装python,再安装再研究感觉有点费劲,于是就花了半小时做了这个爬虫,技术一般,只记录分析实现过程,大牛请绕行。其中用到了一些库,包括:jsoup、HttpClient、net.sf.json大家可以自行去下载jar包。

1、分析是否能获得TOP500歌单

首先,打开酷狗首页查看酷狗TOP500,说好的500首,怎么就只有22首呢?

是真的只让看这些还是能找到其余的呢,于是我就看了下这TOP500的链接:

https://www.kugou.com/yy/rank/home/1-8888.html?from=rank

可以看的出home后边有个1,难道这是代表第一页的意思?于是我就把1改成2,进入,果然进入了第二页, 至此可以知道我们可以在网页里获取这500首的歌单。

2、分析找到真正的mp3下载地址(这个有点绕)

点一个歌曲进入播放页面,使用谷歌浏览器的控制台的Elements,搜一下mp3,很轻松就定位到了MP3的位置。

但是使用java访问的时候爬取的html里却没有该mp3的文件地址,那么这肯定是在该页面的位置使用了js来加载mp3,那么刷新下网页,看网页加载了哪些东西,加载的东西有点多,着重看一下js、php的请求,主要是看里面有没有mp3的地址,分析细节就不用说了。

最终我在列表的

https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191027067069941080546_1546235744250&hash=667939C6E784265D541DEEE65AE4F2F8&album_id=0&_=1546235744251

这个请求里发现了mp3的完整地址:

"play_url": "http:\/\/fs.w.kugou.com\/201812311325\/dcf5b6449160903c6ee48035e11434bb\/G128\/M08\/02\/09\/IIcBAFrZqf2ANOadADn94ubOmaU995.mp3",

那这个js是怎么判断是哪首歌的呢,那么只可能是hash这个参数来决定歌曲的,然后到播放页面里找到这个hash的位置,是在下面的js里:

var dataFromSmarty = [{"hash":"667939C6E784265D541DEEE65AE4F2F8","timelength":"237051","audio_name":"\u767d\u5c0f\u767d - \u6700\u7f8e\u5a5a\u793c","author_name":"\u767d\u5c0f\u767d","song_name":"\u6700\u7f8e\u5a5a\u793c","album_id":0}],//当前页面歌曲信息
 playType = "search_single";//当前播放
 </script>

在去java爬取该网页,查看能否爬到这个hash,果然,爬取的html里有这段js,到现在mp3的地址也找到了,歌单也找到了,那么下一步就用程序实现就可以了。

3、java实现爬取酷狗mp3

先看一下爬取结果:

找到了资源,程序实现就好说了,其中使用到了自己写的几个工具类,自己整理点自己的工具类还是有好处的,以后遇到什么问题就没必要重新写了,直接拿来用就可以了。没什么好说的了,下面直接贴出源码。

SpiderKugou.java

package com.bing.spider;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.bing.download.FileDownload;
import com.bing.html.HtmlManage;
import com.bing.http.HttpGetConnect;
import net.sf.json.JSONObject;
public class SpiderKugou {
 public static String filePath = "F:/music/";
 public static String mp3 = "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191027067069941080546_1546235744250&"
 + "hash=HASH&album_id=0&_=TIME";
 
 public static String LINK = "https://www.kugou.com/yy/rank/home/PAGE-8888.html?from=rank";
 //"https://www.kugou.com/yy/rank/home/PAGE-23784.html?from=rank";
 
 
 public static void main(String[] args) throws IOException {
 
 for(int i = 1 ; i < 23 ; i++){
 String url = LINK.replace("PAGE", i + "");
 getTitle(url);
 //download("https://www.kugou.com/song/mfy6je5.html");
 }
 }
 
 public static String getTitle(String url) throws IOException{
 HttpGetConnect connect = new HttpGetConnect();
 String content = connect.connect(url, "utf-8");
 HtmlManage html = new HtmlManage();
 Document doc = html.manage(content);
 Element ele = doc.getElementsByClass("pc_temp_songlist").get(0);
 Elements eles = ele.getElementsByTag("li");
 for(int i = 0 ; i < eles.size() ; i++){
 Element item = eles.get(i);
 String title = item.attr("title").trim();
 String link = item.getElementsByTag("a").first().attr("href");
 
 download(link,title);
 }
 return null;
 }
 
 public static String download(String url,String name) throws IOException{
 String hash = "";
 HttpGetConnect connect = new HttpGetConnect();
 String content = connect.connect(url, "utf-8");
 HtmlManage html = new HtmlManage();
 
 String regEx = "\"hash\":\"[0-9A-Z]+\"";
 // 编译正则表达式
 Pattern pattern = Pattern.compile(regEx);
 Matcher matcher = pattern.matcher(content);
 if (matcher.find()) {
 hash = matcher.group();
 hash = hash.replace("\"hash\":\"", "");
 hash = hash.replace("\"", "");
 }
 
 String item = mp3.replace("HASH", hash);
 item = item.replace("TIME", System.currentTimeMillis() + "");
 System.out.println(item);
 String mp = connect.connect(item, "utf-8");
 
 mp = mp.substring(mp.indexOf("(") + 1, mp.length() - 3);
 
 JSONObject json = JSONObject.fromObject(mp);
 String playUrl = json.getJSONObject("data").getString("play_url");
 
 System.out.print(playUrl + " == ");
 FileDownload down = new FileDownload();
 down.download(playUrl, filePath + name + ".mp3");
 
 System.out.println(name + "下载完成");
 return playUrl;
 }
}

HttpGetConnect.java

package com.bing.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.params.HttpParams; 
/**
* @说明:
* @author: gaoll
* @CreateTime:2014-11-13
* @ModifyTime:2014-11-13
*/
public class HttpGetConnect {
 
 /**
 * 获取html内容
 * @param url
 * @param charsetName UTF-8、GB2312
 * @return
 * @throws IOException
 */
 public static String connect(String url,String charsetName) throws IOException{
 BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
 
 CloseableHttpClient httpclient = HttpClients.custom()
 .setConnectionManager(connManager)
 .build();
 String content = "";
 
 try{
 HttpGet httpget = new HttpGet(url);
 
 RequestConfig requestConfig = RequestConfig.custom()
 .setSocketTimeout(5000)
 .setConnectTimeout(50000)
 .setConnectionRequestTimeout(50000)
 .build();
 httpget.setConfig(requestConfig);
 httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
 httpget.setHeader("Accept-Encoding", "gzip,deflate,sdch");
 httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
 httpget.setHeader("Connection", "keep-alive");
 httpget.setHeader("Upgrade-Insecure-Requests", "1");
 httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
 //httpget.setHeader("Hosts", "www.oschina.net");
 httpget.setHeader("cache-control", "max-age=0"); 
 
 CloseableHttpResponse response = httpclient.execute(httpget);
 
 int status = response.getStatusLine().getStatusCode();
 if (status >= 200 && status < 300) {
 
 HttpEntity entity = response.getEntity();
 InputStream instream = entity.getContent();
 BufferedReader br = new BufferedReader(new InputStreamReader(instream,charsetName));
 StringBuffer sbf = new StringBuffer();
 String line = null;
 while ((line = br.readLine()) != null){
 sbf.append(line + "\n");
 }
 br.close();
 content = sbf.toString();
 } else {
 content = "";
 }
 
 }catch(Exception e){
 e.printStackTrace();
 }finally{
 httpclient.close();
 }
 //log.info("content is " + content);
 return content;
 }
 private static Log log = LogFactory.getLog(HttpGetConnect.class);
}

HtmlManage.java

package com.bing.html;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.bing.http.HttpGetConnect;
/**
* @说明:
* @author: gaoll
* @CreateTime:2014-11-13
* @ModifyTime:2014-11-13
*/
public class HtmlManage {
 
 public Document manage(String html){
 Document doc = Jsoup.parse(html);
 return doc;
 }
 
 public Document manageDirect(String url) throws IOException{
 Document doc = Jsoup.connect( url ).get();
 return doc;
 }
 
 public List<String> manageHtmlTag(Document doc,String tag ){
 List<String> list = new ArrayList<String>();
 
 Elements elements = doc.getElementsByTag(tag);
 for(int i = 0; i < elements.size() ; i++){
 String str = elements.get(i).html();
 list.add(str);
 }
 return list;
 }
 
 public List<String> manageHtmlClass(Document doc,String clas ){
 List<String> list = new ArrayList<String>();
 
 Elements elements = doc.getElementsByClass(clas);
 for(int i = 0; i < elements.size() ; i++){
 String str = elements.get(i).html();
 list.add(str);
 }
 return list;
 }
 
 public List<String> manageHtmlKey(Document doc,String key,String value ){
 List<String> list = new ArrayList<String>();
 
 Elements elements = doc.getElementsByAttributeValue(key, value);
 for(int i = 0; i < elements.size() ; i++){
 String str = elements.get(i).html();
 list.add(str);
 }
 return list;
 }
 
 private static Log log = LogFactory.getLog(HtmlManage.class);
}

FileDownload.java

package com.bing.download;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
* @说明:
* @author: gaoll
* @CreateTime:2014-11-20
* @ModifyTime:2014-11-20
*/
public class FileDownload {
 
 /**
 * 文件下载
 * @param url 链接地址
 * @param path 要保存的路径及文件名
 * @return
 */
 public static boolean download(String url,String path){
 
 boolean flag = false;
 
 CloseableHttpClient httpclient = HttpClients.createDefault();
 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000)
 .setConnectTimeout(2000).build();
 HttpGet get = new HttpGet(url);
 get.setConfig(requestConfig);
 
 BufferedInputStream in = null;
 BufferedOutputStream out = null;
 try{
 for(int i=0;i<3;i++){
 CloseableHttpResponse result = httpclient.execute(get);
 System.out.println(result.getStatusLine());
 if(result.getStatusLine().getStatusCode() == 200){
 in = new BufferedInputStream(result.getEntity().getContent());
 File file = new File(path);
 out = new BufferedOutputStream(new FileOutputStream(file));
 byte[] buffer = new byte[1024];
 int len = -1;
 while((len = in.read(buffer,0,1024)) > -1){
 out.write(buffer,0,len);
 }
 flag = true;
 break;
 }else if(result.getStatusLine().getStatusCode() == 500){
 continue ;
 }
 }
 
 }catch(Exception e){
 e.printStackTrace();
 flag = false;
 }finally{
 get.releaseConnection();
 try{
 if(in != null){
 in.close();
 }
 if(out != null){
 out.close();
 }
 }catch(Exception e){
 e.printStackTrace();
 flag = false;
 }
 }
 return flag;
 }
 private static Log log = LogFactory.getLog(FileDownload.class);
}

到这就结束了,有可能有些代码没贴全,主要代码已经差不多,应该可以跑起来,多多指教。

(完)

来源:my.oschina.net/gllfeixiang/blog/2995570?p=1

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码