Jsoup爬取一首音乐

一、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;

/**
 * 爬取网易云音乐
 *
 * @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) {
        // 1. 获取界面信息
        Document document = getDocument(url);
        //System.out.println(document.body());

        // 2. 获取专辑地址信息
        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);
            }
        });

        // 3. 获取专辑歌曲信息
        Set<String> songUrls = new HashSet<>();
        Map<String, String> nameMap = new HashMap<>();
        albumUrls.forEach(albumUrl -> {
            Document document1 = getDocument(albumUrl);
            // 歌曲id
            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());

        // 4. 下载歌曲信息
        songUrls.forEach(songUrl -> downloadSong(songUrl, nameMap.get(songUrl)));
    }

    /**
     * 获取url中的 id 参数的值
     * @param url 地址
     * @return id信息
     */
    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 歌曲地址
     * @param songName 歌曲名称
     */
    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();
        }
        // 设置song文件名
        // String fileName = getIdOfUrl(songUrl);
        // 写出的路径
        File file = new File(filePath + File.separator + songName + ".mp3");

        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 (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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值