首页 > Python资料 博客日记
python---爬取QQ音乐
2024-09-06 16:00:07Python资料围观87次
文章python---爬取QQ音乐分享给大家,欢迎收藏Python资料网,专注分享技术知识
如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")
如果有所帮助请给个免费的赞吧~有人看才是支撑我写下去的动力!
声明:
本文仅用作学习记录和交流
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- 【Python】selenium安装+Microsoft Edge驱动器下载配置流程
- Python 中自动打开网页并点击[自动化脚本],Selenium
- Anaconda基础使用
- 【Python】成功解决 TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
- manim边学边做--三维的点和线
- CPython是最常用的Python解释器之一,也是Python官方实现。它是用C语言编写的,旨在提供一个高效且易于使用的Python解释器。
- Anaconda安装配置Jupyter(2024最新版)
- Python中读取Excel最快的几种方法!
- Python某城市美食商家爬虫数据可视化分析和推荐查询系统毕业设计论文开题报告
- 如何使用 Python 批量检测和转换 JSONL 文件编码为 UTF-8
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Anaconda版本和Python版本对应关系(持续更新...)
- Python pyinstaller打包exe最完整教程
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj