import requests, json, threading, queue, os, random, time, re import urllib.request import pandas as pd class ThreadDown(threading.Thread): def __init__(self, urlQueue, path, lock): super(ThreadDown, self).__init__() self.urlQueue = urlQueue self.path = path self.lock = lock def run(self): while not self.urlQueue.empty(): song_data = self.urlQueue.get() songName = re.sub(r'[\/:*?"<>|]', '-', song_data['fileName']) fileName = os.path.join(self.path, songName + '.mp3') url = song_data['downloadUrl'] try: urllib.request.urlretrieve(url, fileName) except: print('%s 下载失败!' % songName) with self.lock: txtFile = os.path.join(self.path, 'aNote.txt') with open(txtFile, 'a') as f: f.write(songName + '-' + url + '\n') time.sleep(random.randint(3, 10)) headers={ 'UserAgent' : 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3', 'Referer' : 'http://m.kugou.com/rank/info/8888', 'Cookie' : 'UM_distinctid=161d629254c6fd-0b48b34076df63-6b1b1279-1fa400-161d629255b64c; kg_mid=cb9402e79b3c2b7d4fc13cbc85423190; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1523818922; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1523819865; Hm_lvt_c0eb0e71efad9184bda4158ff5385e91=1523819798; Hm_lpvt_c0eb0e71efad9184bda4158ff5385e91=1523820047; musicwo17=kugou' } def get_songs(url): res=requests.get(url,headers=headers) return res.text def get_song_download_url(url): res=requests.get(url,headers=headers) res_tmp_list = json.loads(res.text) return res_tmp_list['data']['play_url'] def get_song_page_data(url): Song_Json = json.loads(get_songs(URL)) Song_List_Json = Song_Json['data']['info'] total = [] for i in range(len(Song_List_Json)): song_download_url = "http://www.kugou.com/yy/index.php?r=play/getdata&hash=%s&album_id=%s&_=1523819864065" % (Song_List_Json[i]['hash'], Song_List_Json[i]['album_id']) song_data_dict = {'downloadUrl':get_song_download_url(song_download_url),'fileName':Song_List_Json[i]['filename']} total.append(song_data_dict) return total if __name__ == '__main__': if not os.path.exists('KuGou'): os.mkdir('KuGou') path = os.path.join(os.getcwd(), 'KuGou') thread_num = 30 lock = threading.Lock() urlQueue = queue.Queue() for i in range(1, 11): URL='http://mobilecdngz.kugou.com/api/v3/rank/song?rankid=8888&ranktype=2&page=%s&pagesize=50&volid=&plat=2&version=8955&area_code=1' % i page_list_data = get_song_page_data(URL) for page_data in page_list_data: urlQueue.put(page_data) # print("%s %s" % (page_list_data[j]['fileName'],page_list_data[j]['downloadUrl'])) threadList = [] for i in range(thread_num): thread = ThreadDown(urlQueue, path, lock) thread.start() threadList.append(thread) for thread in threadList: thread.join() print('全部下载完成!')