Python – KuWo music

Time:2021-6-18

Complete code

import requests
import json
import os
Process of foreign exchange deposit and withdrawal https://www.fx61.com/support

def music_download():
    KW = input ("please input music name:)
    #Request header
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36 Edg/84.0.522.63",
        "Cookie":"_ga=GA1.2.1083049585.1590317697; _gid=GA1.2.2053211683.1598526974; _gat=1; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1597491567,1598094297,1598096480,1598526974; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1598526974; kw_token=HYZQI4KPK3P",
        "Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
        "csrf": "HYZQI4KPK3P",
    }
    #Parameter list
    params = {
        "key": kw,
        #Number of pages
        "pn": "1",
        #Number of music
        "rn": "10",
        "httpsStatus": "1",
        "reqId": "cc337fa0-e856-11ea-8e2d-ab61b365fb50",
    }
    #Create a list and download it later
    music_list = []
    url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?"
    res = requests.get(url = url,headers = headers,params = params)
    res.encoding = "utf-8"
    text = res.text
    #Convert to JSON data
    json_list = json.loads(text)
    #It is found that the list in data is the place where the main data is stored
    datapack = json_list["data"]["list"]
    #Traverse to get the required data, music name, singer, ID
    for i in datapack:
        #Music name
        music_name = i["name"]
        #Singer
        music_singer = i["artist"]
        #I'll get the ID I need later
        rid = i["rid"]
        #Try to listen to any music, get a music interface, this is the rid can be used
        api_music = "http://www.kuwo.cn/url?format=mp3&rid={}&response=url&type=convert_url3" 
                    "&br=128kmp3&from=web&t=1598528574799&httpsStatus=1" 
                    "&reqId=72259df1-e85a-11ea-a367-b5a64c5660e5".format(rid)
        api_res = requests.get(url = api_music)
        #Print found that the real URL is really inside
        # print(api_res.text)
        music_url = json.loads(api_res.text)["url"]
        #It's done. Try the effect
        print(music_name)
        print(music_singer)
        print(music_url)
        #Save the data to the dictionary to search when downloading
        music_dict = {}
        music_dict["name"] = music_name
        music_dict["url"] = music_url
        music_dict["singer"] = music_singer
        music_list.append(music_dict)
    #Look at the amount of real data
    print(len(music_list))
    #Download
    Xiazai = input ("input music name:)
    #Download位置
    Root ='e // downloaded music // '
    for i in range(len(music_list)):
        try:
            if xiazai == music_list[i]["name"]:
                #Create folder
                if not os.path.exists(root):
                    os.mkdir(root)
                #Get the corresponding music URL data in the dictionary
                music_content = requests.get(url = music_list[i]["url"]).content
                with open(root + "{}({}).mp3".format(music_list[i]['name'],music_list[i]['singer']),"wb") as f:
                    f.write(music_content)
                    Print ("download successful")
            else:
                Print ("this song name is not in the music you are searching for!")
                continue
        except:
            Print ("download failed")
if __name__ == "__main__":
    music_download()