python---爬取QQ音乐

如Cookie为非vip,仅能获取非vip歌曲

1.下载包

python 复制代码
pip install jsonpath 

2.代码

python 复制代码
import os
import time
import requests
from jsonpath import jsonpath

def search_and_download_qq_music(query_text):

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
        'Cookie': 'you cookie here'
    }

    # 请求的链接
    # query_text = 'Black Myth wukong'
    search_url = rf'https://u.y.qq.com/cgi-bin/musicu.fcg?data={{"comm":{{"g_tk":235530277,"uin":"1152921504916411742","format":"json","inCharset":"utf-8","outCharset":"utf-8","notice":0,"platform":"h5","needNewCode":1,"ct":23,"cv":0}},"req_0":{{"method":"DoSearchForQQMusicDesktop","module":"music.search.SearchCgiService","param":{{"remoteplace":"txt.mqq.all","searchid":"64237725668973550","search_type":0,"query":"{query_text}","page_num":1,"num_per_page":20}}}}}}'

    response = requests.get(search_url)
    music_list = jsonpath(response.json(), '$..data.body.song.list')[0]
    # print(f'music_list: {music_list}')

    if not os.path.exists('Q Music'):
        os.mkdir('Q Music')

    for item in music_list:
        music_mid = jsonpath(item, '$.mid')[0]
        music_name = jsonpath(item, '$.name')[0]

        # print(f'music_name: {music_name}, music_mid: {music_mid}')

        music_data_url = rf'https://u.y.qq.com/cgi-bin/musicu.fcg?data={{"comm":{{"cv":4747474,"ct":24,"format":"json","inCharset":"utf-8","outCharset":"utf-8","notice":0,"platform":"yqq.json","needNewCode":1,"uin":"1152921504916411742","g_tk_new_20200303":1849600344,"g_tk":1849600344}},"req_9":{{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{{"guid":"4868259520","songmid":["{music_mid}"],"songtype":[0],"uin":"1152921504916411742","loginflag":1,"platform":"20"}}}}}}'
        music_data_response = requests.get(music_data_url)
        # print(f'music_data_response: {music_data_response.json()}')

        data_info = jsonpath(music_data_response.json(), '$..purl')[0]
        # print(f'data_info: {data_info}')

        music_url = f'https://dl.stream.qqmusic.qq.com/{data_info}'

        music_response = requests.get(music_url, headers=headers)

        with open(f'./Q Music/{music_name}.mp3', 'wb') as file:
            file.write(music_response.content)

        print(f'《{music_name}》下载成功')
        print("-" * 20)
        time.sleep(0.5)

        # break

search_and_download_qq_music("Black Myth wukong")

如果有所帮助请给个免费的赞吧~有人看才是支撑我写下去的动力!

声明:

本文仅用作学习记录和交流

相关推荐
KevinRay_14 分钟前
Python超能力:高级技巧让你的代码飞起来
网络·人工智能·python·lambda表达式·列表推导式·python高级技巧
Captain823Jack1 小时前
nlp新词发现——浅析 TF·IDF
人工智能·python·深度学习·神经网络·算法·自然语言处理
资源补给站1 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
Captain823Jack1 小时前
w04_nlp大模型训练·中文分词
人工智能·python·深度学习·神经网络·算法·自然语言处理·中文分词
PieroPc2 小时前
Python 自动化 打开网站 填表登陆 例子
运维·python·自动化
VinciYan2 小时前
基于Jenkins+Docker的自动化部署实践——整合Git与Python脚本实现远程部署
python·ubuntu·docker·自动化·jenkins·.net·运维开发
测试老哥3 小时前
外包干了两年,技术退步明显。。。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
終不似少年遊*3 小时前
美国加州房价数据分析01
人工智能·python·机器学习·数据挖掘·数据分析·回归算法
如若1233 小时前
对文件内的文件名生成目录,方便查阅
java·前端·python
西猫雷婶4 小时前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv