qq音乐排行榜数据爬取

废话不多说,直接上教程
qq音乐排行榜链接:
https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&inCharset=utf8&outCharset=utf-8platform=yqq.json&needNewCode=0&data={“detail”:{“module”:“musicToplist.ToplistInfoServer”,“method”:“GetDetail”,“param”:{“topId”:26,”offset”:0,“num”:20,“period”:“2019-12-20”}}}
其中topId为榜单id,period为日期,num为获取榜单歌曲数,基于这些信息,我们通过修改这三个参数就可以很轻易地获取其他榜单的任意日期的数据,获取之后通过json解析工具即可获取榜单中的歌曲信息,下面以qq流行指数榜为例,上源码。


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtil {
    /**
     * http get服务
     *
     * @param httpUrl url
     * @return 响应结果
     */
    public static String doGet(String httpUrl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        // 返回结果字符串
        String result = null;
        try {
            // 创建远程url连接对象
            URL url = new URL(httpUrl);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection)url.openConnection();
            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is,并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// 关闭远程连接
        }

        return result;
    }

    public static void main(String[] args) {
        String url = "https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&inCharset=utf8&outCharset=utf-8&data=%7B%22detail%22:%7B%22module%22:%22musicToplist.ToplistInfoServer%22,%22method%22:%22GetDetail%22,%22param%22:%7B%22topId%22:4,%22offset%22:0,%22num%22:120,%22period%22:%222019-12-20%22%7D%7D%7D";
        String response = HttpUtil.doGet(url);
        System.out.println(response);
    }
}

有什么问题或者爬虫需求,欢迎评论区留言

你可能感兴趣的:(爬虫,java,爬虫)