Python下載網易雲歌單歌曲的示例程式碼
阿新 • • 發佈:2020-08-14
今天寫了個下載指令碼,記錄一下
效果:
直接上程式碼:
# 網易雲 根據歌單鏈接下載MP3歌曲 import requests from bs4 import BeautifulSoup def main(): url = "https://music.163.com/#/playlist?id=3136952023" # 歌單地址 請自行更換 if '/#/' in url: url = url.replace('/#/','/') headers = { 'Referer': 'http://music.163.com/','Host': 'music.163.com','cookie': '自己去網站拿,獲取方式在下邊','User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/84.0.4147.105 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',} s = requests.session() try: response = s.get(url,headers=headers).content soup = BeautifulSoup(response,'lxml') lis = list(soup.find('ul')) fatherlis = ['歌單名:' + str(soup.find('h2').string)] for i in lis: sonlis = [] sonlis.append(str(len(fatherlis)) + '.') sonlis.append(i.a.string) sonlis.append(str(i.a.get('href'))[str(i.a.get('href')).find('=') + 1:-1] + str(i.a.get('href'))[-1]) fatherlis.append(sonlis) except: print("\n\t歌曲連結輸入錯誤") exit('ERROR!') format = '{0:<10}\t{1:{3}<10}\t{2:<10}' print("從'{}'中找到了{}條歌曲".format(str(soup.find('h2').string),len(fatherlis) - 1)) print('-------------------------------------------------------------------------------------------------') print('序號 歌曲名稱 歌曲連結') for i in fatherlis: if fatherlis.index(i) == 0: continue else: print( format.format(i[0],i[1],'http://music.163.com/song/media/outer/url?id=' + i[2] + '.mp3',chr(12288))) download_music(i[2],i[1]) print('##########################下載完成##########################') # 歌曲下載 def download_music(song_id,song_name): file = "./music/" # 儲存音樂的檔案路徑 wurl = "https://link.hhtjim.com/163/" # 外鏈地址 song_url = wurl + song_id + ".mp3" # 獲取歌曲16進位制編碼 song = requests.get(song_url).content # 獲取歌曲名稱 # 儲存檔案 with open(file + song_name + '.mp3','wb') as f: f.write(song) if __name__ == '__main__': main()
cookie獲取方式
登入網易雲web版 https://music.163.com/
複製貼上到上方程式碼中,開始下載就好了
到此這篇關於Python下載網易雲歌單歌曲的示例程式碼的文章就介紹到這了,更多相關Python下載網易雲歌單內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!