启动类:

package com.tianbo.second;import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;
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;
import org.apache.http.impl.entity.LaxContentLengthStrategy;
import org.apache.http.util.EntityUtils;
import org.hamcrest.CoreMatchers;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;/*** @说明:主启动类*/
public class Download_KuGou_2021 {public static String filePath = "E:\\\\java_paChong\\\\kugou_music"; static String  top2 ="&dfid=33V6Po0SomVY3diAxp0AWXJM&mid=18cec7cb0bc0e9602342c459c61adfa4&platid=4";static String  album_ID="album_ID";//   https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery19107190036980605969_1611650526484&//hash=F8784EBFBAE36B324EC1E3441B6156B4 &dfid=33V6Po0SomVY3diAxp0AWXJM&mid=18cec7cb0bc0e9602342c459c61adfa4&platid=4&album_id=41163623&_=1611650526485public static   String mp3 = "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery19107190036980605969_1611650526484&"+ "hash=HASH"+ top2 + "&album_id="+album_ID +"&_=TIME";;public static String LINK = "https://www.kugou.com/yy/rank/home/PAGE-8888.html?from=rank";public static void main(String[] args) throws Exception {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 Exception{Document doc = Jsoup.parse(new URL(url),10000); // 解析网页 得到文档对象Elements ele = doc.select("#rankWrap .pc_temp_songlist li");for (Element e__0:ele){    String title = e__0.attr("title").trim();String link = e__0.getElementsByTag("a").first().attr("href");System.out.println("---title----"+title);System.out.println("---link----"+link);File file2=new File(filePath  + File.separator  + File.separator + title + ".mp3");    if(!file2.exists()) {    download(link,title);}}return "";}public static String download(String url,String name) throws Exception{String hash = "";album_ID="album_ID";String playUrl ="";Document doc = Jsoup.parse(new URL(url),10000); // 解析网页 得到mp3的质地Elements ele = doc.select("html");for (Element e__0:ele){String regEx = "\"hash\":\"[0-9A-Z]+\"";// 编译正则表达式Pattern pattern = Pattern.compile(regEx);Matcher matcher = pattern.matcher(e__0.toString());System.out.println("----album_ID----"+album_ID);if (matcher.find()) {hash = matcher.group();hash = hash.replace("\"hash\":\"", "");hash = hash.replace("\"", "");System.out.println("-----hash-----"+hash);   //F8784EBFBAE36B324EC1E3441B6156B4}int album_id_index=e__0.toString().indexOf("\"album_id\":");//获取下标,截取字符串album_ID = e__0.toString().substring(album_id_index + 11, album_id_index + 11 + 10);album_ID = album_ID.substring(0, album_ID.indexOf("}"));String item_url = mp3.replace("HASH", hash);item_url = item_url.replace("TIME", System.currentTimeMillis() + "");item_url = item_url.replace("album_ID", album_ID);System.out.println("-------item_url------"+item_url);//------------------------------------------------------Document doc__1 = Jsoup.parse(new URL(item_url),10000); // 解析网页 得到mp3的质地Elements e__1 = doc__1.select("html");for (Element e__2:e__1){
//             System.out.println("--------e__2------"+e__2.toString());String e__2__0 = e__2.toString().substring(e__2.toString().indexOf("(") + 1, e__2.toString().length() - 3);
//             System.out.println("------e__2__0-----"+e__2__0);e__2__0 = e__2__0.toString().substring(0, e__2__0.toString().indexOf(");") );System.out.println("------e__2__0--111111111---"+e__2__0);playUrl = e__2__0.toString().substring(e__2__0.toString().indexOf("\"play_url\""), e__2__0.toString().indexOf("\"authors\""));System.out.println("------playUrl-----"+playUrl);playUrl = playUrl.substring(12,playUrl.toString().indexOf("\","));System.out.println("------playUrl-----"+playUrl);FileDownload down = new FileDownload();File file_xiazai=new File(filePath  + File.separator  + File.separator + name + ".mp3");    if(!file_xiazai.exists()) {System.out.println("----------file_xiazai------"+filePath  + File.separator  + File.separator + name + ".mp3");boolean download = down.download(playUrl, filePath  + File.separator  + File.separator + name + ".mp3");System.out.println("-----download-------"+download);if(download==false) {continue;}System.out.println(name + ",下载完成");}}}return playUrl;}}

下载:

package com.tianbo.second;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;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;/*** @说明:文件下载*/
public class FileDownload {/*** 文件下载* @param url 链接地址* @param path 要保存的路径及文件名* @return* @throws Exception */public static boolean download(String url,String path) {//      System.out.println("----url---"+url);url=url.replace("\\","/");
//      System.out.println("----url---"+url);url=url.replace("","//");System.out.println("----url---"+url);
//
//      System.out.println("----path---"+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() == 403){System.out.println("-----下载---错误----"+403);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);
}

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tianbo</groupId><artifactId>maven-pachong-second</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>maven-pachong-second Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!-- httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.12</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.11.0</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><!-- servlet依赖的jar包start --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><!-- servlet依赖的jar包start --><!-- jsp依赖jar包start --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version></dependency><!--jstl标签依赖的jar包start --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.8.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.11</version></dependency><!-- json 处理 --><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency></dependencies><build><finalName>maven-pachong-second</finalName></build>
</project>

2021-下载酷狗音乐-爬虫-java相关推荐

  1. 一篇文章教会你使用Python网络爬虫下载酷狗音乐

    [一.项目背景] 现在的听歌软件动不动就是各种付费,要下载软件才能听,当你下载了之后,你会惊奇的发现这首歌还收费,这就让一向喜欢白嫖的小编感到很伤心了.于是,小编冥思苦想,终于让我发现了其中的奥秘,一 ...

  2. Java爬取并下载酷狗音乐

    本文方法及代码仅供学习,仅供学习. 案例: 下载酷狗TOP500歌曲,代码用到的代码库包含:Jsoup.HttpClient.fastJson等. 正文: 1.分析是否可以获取到TOP500歌单 打开 ...

  3. python爬取酷狗音乐json数据为空_【Python3爬虫】下载酷狗音乐上的歌曲

    经过测试,可以下载要付费下载的歌曲(n_n) 准备工作:python3.5+pycharm 使用到的库:requests,re,json 步骤: 打开酷狗音乐的官网,输入想要搜索的歌曲(例如<天 ...

  4. 【Python3爬虫】下载酷狗音乐上的歌曲

    经过测试,可以下载要付费下载的歌曲(n_n) 准备工作:Python3.5+Pycharm 使用到的库:requests,re,json,time,fakeuseragent 步骤: 打开酷狗音乐的官 ...

  5. 我是如何利用Python下载酷狗音乐的

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 万战自称不提刃,生来双眼篾群容. ...

  6. 使用Python下载酷狗音乐

    使用Python+Selenium+Urllib下载酷狗歌曲 最近想下载一首歌,找了各大音乐平台,觉得在酷狗上下载更容易. 首先是获取原音频地址(本文以野狼disco为例),存储在<audio& ...

  7. python下载酷狗音乐上的歌曲

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:TM0831 PS:如有需要Python学习资料的小伙伴可以加点击下 ...

  8. Python下载酷狗音乐歌曲

    最近想研究一下Python的图形界面,但是因为我的Python版本是2.7,没法用QT,所以试了试wxPython.效果还不错,虽然总体来说不如WPF好用和美观,但是做简单的应用程序足够了. 作为练习 ...

  9. python自动下载酷狗音乐_使用Python下载酷狗音乐

    使用Python+Selenium+Urllib下载酷狗歌曲 最近想下载一首歌,找了各大音乐平台,觉得在酷狗上下载更容易. 首先是获取原音频地址(本文以野狼disco为例),存储在标签里的src属性中 ...

最新文章

  1. Java 构造方法与成员方法的区别
  2. 细胞内钾多钠少——原初生物的第三大遗迹?
  3. 2014-03-11 Spring的学习(3)------面向切面编程(AOP)
  4. mysql数据库导出后乱码问题_MySQL导入导出数据出现乱码的解决办法
  5. 求连通块个数(使用并查集)
  6. Java如何判断今天本月第几周的周几?
  7. Spring Cloud 搭建 Hystrix Dashboard和Turbine
  8. leetcode 28. 实现 strStr()(kmp)
  9. JavaScript中的“ this”关键字
  10. 压测 mysql关闭连接_MySQL 压测
  11. 编织让你受益一生的交际网
  12. java设计模式案例详解:观察者模式
  13. html php交互json 结果集,基于HTML模板和JSON数据的JavaScript交互(移动端)
  14. lnmp一键安装包 php7,LNMP一键安装包 V1.7 正式版发布
  15. 仙童的ua741运算放大器内部电路
  16. 搞着玩:基于Spring Boot的企业CMS系统
  17. 别踩白块游戏java项目总结_学习小游戏别踩白块总结
  18. 如何通过软件实现自动阶梯计电梯费
  19. 深度学习CPU版本环境搭建(从anaconda->pycharm->tensorflow)
  20. leaflet运动轨迹

热门文章

  1. 《零基础学C语言》前言
  2. 掌握SQL Monitoring这些特性,SQL优化通通不在话下
  3. 基于java的滑雪场学具租赁管理系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  4. java计算机毕业设计商品供应管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  5. 酷派大观4 8970 刷android 4.4,极速达百兆! 移动4G版酷派大观4网络体验
  6. Oracle ERP 仓库(inventory) 词汇1
  7. 2k2实用球员_NBA2K Online2实用平民球员推荐:上篇
  8. 怎么找电脑服务器文档,怎么找到电脑的服务器地址
  9. 浅谈信息安全的职业发展方向规划(乙方安全公司篇)
  10. windows 通过cmd命令连接wifi