编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

如何批量获取指定歌手的音乐

wxchong 2025-01-17 13:16:46 开源技术 53 ℃ 0 评论

#头条创作挑战赛#

本文使用python批量获取指定歌手的音乐,并下载到本地。

1. 分析网页

登录酷狗音乐,输入想搜索的歌手的音乐(例如本文的林俊杰),页面结果如下


1.1 分析对应的url

可见,获取歌曲列表的url

list_murl='https://complexsearch.kugou.com/v2/search/song?callback=callback123&srcappid=2919&clientver=1000&clienttime=1669552869004&mid=6fb3e7f78cfb475ec8353739280ca9e0&uuid=6fb3e7f78cfb475ec8353739280ca9e0&dfid=4XSsT32VYg1y0xqt0R3rCVdv&keyword=林俊杰&page=1&pagesize=30&bitrate=0&isfuzzy=0&inputtype=0&platform=WebFilter&userid=0&iscorrection=1&privilege_filter=0&filter=10&token=&appid=1014&signature=f09af4c2d8fd21a095c14300772703ce'


对应的歌曲数据列表

1.2 选择对应歌曲

选择一首歌曲(例如《背对背拥抱》),找到对应的歌曲的url

其中hash值即之前list中的值,由此可将其作为变量传入

for i, s in enumerate(song_list):
    print(f'{i+1} . {s.get("SongName")}---{s.get("FileHash")}--{s.get("AlbumID")}')
    info_url = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={s.get("FileHash")}&album_id={s.get("AlbumID")}'
    # print(info_url)


2. 完整案例代码

2.1 完整案例代码如下

import requests
import json
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
    'Cookie':'kg_mid=6fb3e7f78cfb475ec8353739280ca9e0; kg_dfid=4XSsT32VYg1y0xqt0R3rCVdv; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1669429609,1669430050,1669474232; kg_mid_temp=6fb3e7f78cfb475ec8353739280ca9e0; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1669480245'
}




list_murl='https://complexsearch.kugou.com/v2/search/song?callback=callback123&srcappid=2919&clientver=1000&clienttime=1669552869004&mid=6fb3e7f78cfb475ec8353739280ca9e0&uuid=6fb3e7f78cfb475ec8353739280ca9e0&dfid=4XSsT32VYg1y0xqt0R3rCVdv&keyword=林俊杰&page=1&pagesize=30&bitrate=0&isfuzzy=0&inputtype=0&platform=WebFilter&userid=0&iscorrection=1&privilege_filter=0&filter=10&token=&appid=1014&signature=f09af4c2d8fd21a095c14300772703ce'
m_res = requests.get(list_murl, headers=headers)


#print(m_res.text)


song_list = json.loads(m_res.text[12:-2])['data']['lists']
print(song_list)


for i, s in enumerate(song_list):
    print(f'{i+1} . {s.get("SongName")}---{s.get("FileHash")}--{s.get("AlbumID")}')
    info_url = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={s.get("FileHash")}&album_id={s.get("AlbumID")}'
    # print(info_url)
    f_name = f'{i+1}. {s.get("SongName")}.mp3'
    m_res2 = requests.get(info_url, headers=headers)
    print(f'{i+1} .{m_res2.json()["data"]["play_url"]}')
    m_url = m_res2.json()['data']['play_url']
    # print(m_url)
    if m_url != '':
        m_res3 = requests.get(m_url, headers=headers)
        with open(f_name, 'wb') as f:
            f.write(m_res3.content)

2.2 运行结果

运行过程中会将对应歌曲的url打印出来


3. ChatGPT方案


由此可见,ChatGPT 可以在我们写代码时提供一些参考的方法。

PS:以上是参考尚学堂项目案例学习编写,仅供学习使用,严禁用于商业用途或破坏性用途。祝愿大家听歌无忧~~~

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表