qq音乐排行榜数据爬取

该博客分享了如何利用Java实现从QQ音乐获取排行榜数据的教程。通过修改URL中的topId、period和num参数,可以获取不同榜单和日期的歌曲信息。示例代码展示了获取QQ流行指数榜的方法,涉及HTTP GET请求和JSON解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说,直接上教程
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);
    }
}

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

以下是使用Python和Pycharm爬取QQ音乐排行榜的代码: ``` import requests import json # 设置请求头 headers = { 'Referer': 'https://y.qq.com/n/yqq/toplist/4.html', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } # 获取排行榜页面 response = requests.get('https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2021_30&topid=4&type=top&song_begin=0&song_num=30&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0', headers=headers) # 解析json数据 data = json.loads(response.text) # 遍历歌曲列表 for music in data['songlist']: # 获取歌曲mid song_mid = music['data']['mid'] # 构造获取vkey的url url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?callback=getplaysongvkey6354954596402418&g_tk=5381&jsonpCallback=getplaysongvkey6354954596402418&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22583432000%22%2C%22songmid%22%3A%5B%22{}%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%7D'.format(song_mid) # 发送获取vkey的请求 response = requests.get(url, headers=headers) # 解析json数据 data = json.loads(response.text.strip('getplaysongvkey6354954596402418(').strip(')')) # 获取vkey vkey = data['req_0']['data']['midurlinfo'][0]['purl'] # 输出歌曲名和播放链接 print(music['data']['songname'], 'https://isure.stream.qqmusic.qq.com/{}?vkey={}&guid=538432000&uin=0&fromtag=66'.format(vkey, data['req_0']['data']['sip'][0])) ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挖你家服务器电缆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值