当前位置: 首页 > article >正文

python---爬取QQ音乐

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

1.下载包

pip install jsonpath 

2.代码

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")

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

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


http://www.kler.cn/a/291112.html

相关文章:

  • datasets 笔记:加载数据集(基本操作)
  • 写SQL太麻烦?免费搭建 Text2SQL 应用,智能写 SQL | OceanBase AI 实践
  • 小红书关键词搜索采集 | AI改写 | 无水印下载 | 多维表格 | 采集同步飞书
  • 深度学习推理速度优化指南
  • 如何利用Python爬虫获得Lazada商品评论列表
  • springboot中Controller内文件上传到本地以及阿里云
  • 时序预测 | 基于MAMbaS+transformer时间序列预测模型(pytorch)
  • 【JAVA入门】Day32 - 可变参数
  • Readelf 获取Android So `.note.android.ident`
  • C#自定义控件的放置与拖动
  • oh my posh随机选择主题
  • 大坝安全监测设备的工作原理
  • 如何使用 Ruby 中的 Selenium 解决 CAPTCHA
  • STM32的窗口看门狗详解及案例(基于HAL库)
  • Python知识点:如何使用Python实现语音识别
  • imap发送邮件:如何配置IMAP服务器发邮件?
  • 基础学习之——Apache Spark
  • 【项目一】基于pytest的自动化测试框架day1
  • 中锂天源卡车电瓶:绿色能源驱动未来物流
  • go常用代码
  • 数据仓库系列18:如何建立一个有效的元数据管理体系?
  • django之ForeignKey、OneToOneField 和 ManyToManyField
  • 惠中科技综合光伏清洗技术:引领绿色清洁新时代
  • 【后端开发】PHP、go语言、Java、C++、Linux开发等急招中......
  • Windows电脑获取目录及子目录结构及包含文件名的命令
  • notepad++将换行替换成空