Python爬虫類高効率爬取酷我音楽---高音質音楽


'''
#  
1.    url
2.    
3.    
4.    
'''
##**Python           ---     **
#           ID        ,     
import requests
import json             #   requests  json         

#    ,    
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
    'Referer': 'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'csrf': 'RUJ53PGJ4ZD',
    'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1577029678,1577034191,1577034210,1577076651; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1577080777; kw_token=RUJ53PGJ4ZD',
}  #  ,,   F12--Network--  Name---   Headers--- Requests Headers--- User-Agent,Referer,csrf


#         

#      
def get_music(rid,name):

    url = " http: // www.kuwo.cn / url?format = mp3 & rid ={} & response = url & type = convert_url3 & br = 128".format(rid)
    relut = requests.get(url,headers = headers).json()
    music_url = relut["url"]

    with open("    /{}.mp3“.format(name)","wb") as f :   #         “    ”   ,        ,   “/”,             
        music = requests.get(music_url).content      #.content           
        f.write(music)
        f.close()
        print("    ")

#          

def main():           #       

    singer = str(input("       :"))
    pagebox = int(input("         :"))
    for page in range(1,pagebox + 1):
        url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn={}&rn=30&reqId=6a22ad90-6381-11ea-ad74-db939e27359f".format(singer, page)
        response = requests.get(url,headers = headers).json()  #.json()        #get     ,   F12--Network--  Name---   Headers---  General--  Request Method         Request URL      
        #print(response)
        data = response['data']['list']
        #print(data)

        for i in data:
            rid = i["rid"]
            name = i["name"]   #    
            get_music(rid,name)
main()