一、Jsoup爬取一首音乐
package cays.music;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.print.Doc;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
/**
* 爬取网易云音乐
*
* @create 2019/8/24
**/
public class NeteaseCloudMusicDemo {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
/**
* 获取网站document
* @param url
* @return
*/
public Document getDocument(String url) {
Document document = null;
try {
document = Jsoup.connect(url)
.header("User-Agent",USER_AGENT)
.timeout(3000).ignoreContentType(true).get();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
/**
* 执行下载
* @param url
*/
public void execute(String url) {
Document document = getDocument(url);
//System.out.println(document.body());
Elements albums = document.select("div.u-cover.u-cover-alb3 a");
System.out.println("albums size : " + albums.size());
Set<String> albumUrls = new HashSet<>();
albums.forEach(song -> {
String albumUrl = song.absUrl("href");
if (!albumUrl.isEmpty()) {
albumUrls.add(albumUrl);
System.out.println(albumUrl);
}
});
Set<String> songUrls = new HashSet<>();
albumUrls.forEach(albumUrl -> {
Document document1 = getDocument(albumUrl);
// 歌曲id
Elements songs = document1.select("ul.f-hide a");
songs.forEach(song -> {
String songUrl = song.absUrl("href");
if (!songUrl.isEmpty()) {
String id = getIdOfUrl(songUrl);
// 歌曲下载地址
String songDownloadUrl = "http://music.163.com/song/media/outer/url?id=" + id + ".mp3";
songUrls.add(songDownloadUrl);
System.out.println("song download url : " + songDownloadUrl);
}
});
});
System.out.println("song size : " + songUrls.size());
songUrls.forEach(songUrl -> {
downloadSong(songUrl);
});
}
/**
* 获取url中的 id 参数的值
* @param url
* @return
*/
public String getIdOfUrl(String url) {
String []baseUrlAndParams = url.split("\\?");
if (baseUrlAndParams.length != 2) {
return "";
}
String []params = baseUrlAndParams[1].split("&");
if (params.length <= 0) {
return "";
}
for (String param : params) {
if (param.contains("id=")) {
return param.split("=")[1];
}
}
return "";
}
/**
* 将song到本地文件夹
* @param songUrl
*/
public void downloadSong(String songUrl) {
// 若指定文件夹没有,则先创建
String filePath = "src\\main\\java\\cays\\music\\mu_li\\";
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
// 设置song文件名
String fileName = getIdOfUrl(songUrl);
// 写出的路径
File file = new File(filePath + File.separator + fileName);
try {
// 获取songURL
URL url = new URL(songUrl);
// 获得连接
URLConnection connection = url.openConnection();
// 设置10秒的相应时间
connection.setConnectTimeout(10 * 1000);
// 获得输入流
InputStream in = connection.getInputStream();
// 获得输出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
// 构建缓冲区
byte[] buf = new byte[1024];
int size;
// 写入到文件
while (-1 != (size = in.read(buf))) {
out.write(buf, 0, size);
}
out.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NeteaseCloudMusicDemo musicDemo = new NeteaseCloudMusicDemo();
// 歌手id
String id = "4292";
// 歌手界面
String url = "https://music.163.com/artist/album?id=" + id + "&limit=120&offset=0";
musicDemo.execute(url);
}
}
二、成功

三、获取名称版
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class NeteaseCloudMusicDemo {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
public Document getDocument(String url) {
Document document = null;
try {
document = Jsoup.connect(url)
.header("User-Agent",USER_AGENT)
.timeout(3000).ignoreContentType(true).get();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
public void execute(String url) {
Document document = getDocument(url);
Elements albums = document.select("div.u-cover.u-cover-alb3 a");
System.out.println("albums size : " + albums.size());
Set<String> albumUrls = new HashSet<>();
albums.forEach(song -> {
String albumUrl = song.absUrl("href");
if (!albumUrl.isEmpty()) {
albumUrls.add(albumUrl);
System.out.println(albumUrl);
}
});
Set<String> songUrls = new HashSet<>();
Map<String, String> nameMap = new HashMap<>();
albumUrls.forEach(albumUrl -> {
Document document1 = getDocument(albumUrl);
Elements songs = document1.select("ul.f-hide a");
List<String> names = songs.eachText();
for (int i = 0; i < songs.size(); i++) {
Element song = songs.get(i);
String songUrl = song.absUrl("href");
if (!songUrl.isEmpty()) {
String id = getIdOfUrl(songUrl);
String songDownloadUrl = "http://music.163.com/song/media/outer/url?id=" + id + ".mp3";
songUrls.add(songDownloadUrl);
nameMap.put(songDownloadUrl, names.get(i));
System.out.println("song download url : " + songDownloadUrl);
}
}
});
System.out.println("song size : " + songUrls.size());
songUrls.forEach(songUrl -> downloadSong(songUrl, nameMap.get(songUrl)));
}
public String getIdOfUrl(String url) {
String []baseUrlAndParams = url.split("\\?");
if (baseUrlAndParams.length != 2) {
return "";
}
String []params = baseUrlAndParams[1].split("&");
if (params.length <= 0) {
return "";
}
for (String param : params) {
if (param.contains("id=")) {
return param.split("=")[1];
}
}
return "";
}
public void downloadSong(String songUrl, String songName) {
String filePath = "src\\main\\resource\\music\\mu_li\\";
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(filePath + File.separator + songName + ".mp3");
try {
URL url = new URL(songUrl);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(10 * 1000);
InputStream in = connection.getInputStream();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1024];
int size;
while (-1 != (size = in.read(buf))) {
out.write(buf, 0, size);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NeteaseCloudMusicDemo musicDemo = new NeteaseCloudMusicDemo();
String id = "4292";
String url = "https://music.163.com/artist/album?id=" + id + "&limit=120&offset=0";
musicDemo.execute(url);
}
}