利用python爬取音乐

介绍

免费或付费的均可下

下载别的歌曲在93修改name变量值即可

WX20230622-223811@2x

环境

python3

requests库

pip install requests

代码

import json
import requests

def SearchMusic(name, num):

    url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord"
    params = {
        "key": name,
        "pn": "1",
        "rn": 20,
        "httpsStatus": "1",
        "reqId": "c79a1220-0f44-11ee-8d2c-15d203a89811"
    }
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Connection": "keep-alive",
        "Cookie": "kw_token=INDZCD5J338",
        "csrf": "INDZCD5J338",
        "DNT": "1",
        "Host": "www.kuwo.cn",
        "Referer": "http://www.kuwo.cn/search/list?key=%E5%8F%AA%E5%9B%A0%E4%BD%A0%E5%A4%AA%E7%BE%8E",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/114.0"
    }

    response = requests.get(url, params=params, headers=headers)

    total = json.loads(response.text)['data']['total']

    if total:

        if int(total) >= num:
            print(f'找到{num}首歌')
            AllMusic(name, num)
        else:
            print(f'最多只有{int(total) + 1}首歌')
            AllMusic(name, int(total) + 1)




def AllMusic(name, num):

    url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord"
    params = {
        "key": name,
        "pn": "1",
        "rn": num,
        "httpsStatus": "1",
        "reqId": "c79a1220-0f44-11ee-8d2c-15d203a89811"
    }
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Connection": "keep-alive",
        "Cookie": "kw_token=INDZCD5J338",
        "csrf": "INDZCD5J338",
        "DNT": "1",
        "Host": "www.kuwo.cn",
        "Referer": "http://www.kuwo.cn/search/list?key=%E5%8F%AA%E5%9B%A0%E4%BD%A0%E5%A4%AA%E7%BE%8E",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/114.0"
    }

    response = requests.get(url, params=params, headers=headers)

    list = json.loads(response.text)['data']['list']

    # print(f'共找到{len(list) + 1}首:{name} 相关歌曲')

    for item in list:

        # print(item)

        name = item['name']
        artist = item['artist']
        album = item['album']
        songTimeMinutes = item['songTimeMinutes']



        rid = item['rid']

        print(f'歌名:{name} 作者:{artist} 专辑:{album} 时长:{songTimeMinutes} ')

        # GetMusic(name=name, mid=rid)





def GetMusic(name, mid):
    url = 'http://www.kuwo.cn/api/v1/www/music/playUrl'

    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Connection": "keep-alive",
        "Cookie": "kw_token=INDZCD5J338",
        "csrf": "INDZCD5J338",
        "DNT": "1",
        "Host": "www.kuwo.cn",
        "Referer": "http://www.kuwo.cn/search/list?key=%E5%8F%AA%E5%9B%A0%E4%BD%A0%E5%A4%AA%E7%BE%8E",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/114.0"
    }

    params = {
        'mid': mid,
        'type': 'convert_url3',
        'httpsStatus': '1',
        'reqId': 'e536e471-0ff8-11ee-8905-cbb8c1776dd3'
    }

    response = requests.get(url=url, params=params, headers=headers)

    try:

        musicUrl = json.loads(response.text)['data']['url']
        # print('爬取成功')
        DownloadMusic(name, musicUrl)
    except Exception as e:
        print("爬取失败:", e)


def DownloadMusic(name, musicUrl):

    save_path = f"./Music/{name}.mp3"
    response = requests.get(musicUrl)
    if response.status_code == 200:
        with open(save_path, 'wb') as file:
            file.write(response.content)
        print("下载成功:" + name + " : " + musicUrl)
    else:
        print("下载失败")



if __name__ == '__main__':
    # 搜索歌名
    name = '只因你太美'

    # 歌曲数量
    num =10

    SearchMusic(name, num)
 
阅读剩余
THE END