需要安装requests、bs4模块
安装方法:cmd进入控制台
pip install requests
pip install bs4
源码:
1 # -*- Coding:utf-8 -*- 2 import requests,os,Json 3 from bs4 import BeautifulSoup 4 5 print( 6 ''' 7 -------------------------------------- 8 9 网易云音乐歌单下载 10 11 by:冷溪凌寒 12 V 1.1 13 -------------------------------------- 14 ''' 15 ) 16 17 18 headers = { 19 'Connection': 'close', 20 'Referer': 'https://music.163.com/', 21 'Host': 'music.163.com', 22 'cookie':'', #自己获取 23 "Accept-EnCoding": "IDentity", 24 "Accept": "text/HTML,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" 25 "user-agent":''#自己获取 26 } 27 s = requests.session() # 保持已登录状态 28 format = '{0:<10}\t{1:{3}<10}\t{2:<10}' # 输出规则 29 30 def get_List(url): # 获取歌单列表 31 try: 32 response = s.get(url, headers=headers).content # 获取网页 33 soup = BeautifulSoup(response, 'HTML.parser') # HTML格式 34 lis = List(soup.find('ul')) # 寻找ul标签 35 father_List = [str(soup.find('h2').string)] # 获取歌单名字到表[0] 36 for i in lis: 37 son_List = [] 38 son_List.append(str(len(father_List)) + '.') # 序号 39 son_List.append(i.a.string) # 歌曲名称 40 son_List.append(str(i.a.get('href'))[str(i.a.get('href')).find('=') + 1:-1] + str(i.a.get('href'))[-1]) # 歌曲链接 41 father_List.append(son_List) # 添加到列表 42 43 except: 44 print("\n\t歌曲链接输入错误") # 错误情况 45 exit('ERROR!') 46 47 print("从'{}'中找到了{}条歌曲".format(str(soup.find('h2').string), len(father_List) - 1)) 48 return father_List 49 50 51 def mkdir(dir):#创建文件夹 52 dir = dir.translate({ord(i): None for i in '/\:*?"<>|'}) # 将 /\:*?"<>| 文件不能命名的符号去除 53 54 isExists=os.path.exists(dir)#判断是否创建了文件夹 55 if not isExists: 56 os.makedirs(dir)#创建文件夹 57 print("创建文件夹'%s',将图片放入'%s'文件夹内。"%(dir,dir)) 58 else: 59 print("已经有'%s'文件夹,将图片放入'%s'文件夹内。"%(dir,dir)) 60 61 62 def download(List):#根据歌曲列表下载歌曲 63 print('---------------------------------------------------------------------------------------------------') 64 print('序号 歌曲名称 歌曲链接') 65 for i in List: 66 if List.index(i)==0: 67 continue 68 else: 69 i[1] = i[1].translate({ord(i): None for i in '/\:*?"<>|'}) # 将 /\:*?"<>| 文件不能命名的符号去除 70 71 print(format.format(i[0], i[1], 'http://music.163.com/song/media/outer/url?ID=' + i[2] + '.mp3', chr(12288))) # 排版 72 # song_url = "https://link.hhtjim.com/163/" + i[2] + ".mp3" # 外链地址 73 74 temp_url = "https://API.imjad.cn/cloudmusic/?type=song&ID=" + i[2] # 外链地址 75 temp_Jsons = requests.get(temp_url).text 76 temp_List=Json.loads(temp_Jsons).get('data') 77 song_url=temp_List[0]["url"] 78 song = requests.get(song_url).content # 获取歌曲源 79 with open(List[0] +'/'+ i[1] + '.mp3', 'wb') as f: # 写入文件夹 80 f.write(song) 81 f.close() 82 83 print('---------------------------------------------------------------------------------------------------') 84 print('下载结束!') 85 86 def download_song(ID,url):#根据歌曲列表下载歌曲 87 response = s.get(url, headers=headers).content # 获取网页 88 soup = BeautifulSoup(response, 'HTML.parser') # HTML格式 89 name = str(soup.find('em').string) # 获取歌曲名 90 print('---------------------------------------------------------------------------------------------------') 91 name = name.translate({ord(i): None for i in '/\:*?"<>|'}) # 将 /\:*?"<>| 文件不能命名的符号去除 92 print(format.format(1, name, 'http://music.163.com/song/media/outer/url?ID=' + ID + '.mp3', chr(12288))) # 排版 93 94 temp_url = "https://API.imjad.cn/cloudmusic/?type=song&ID=" + ID # 外链地址 95 temp_Jsons = requests.get(temp_url).text 96 temp_List=Json.loads(temp_Jsons).get('data') 97 song_url=temp_List[0]["url"] 98 song = requests.get(song_url).content # 获取歌曲源 99 with open(name + '.mp3', 'wb') as f: # 写入文件夹100 f.write(song)101 f.close()102 103 print('---------------------------------------------------------------------------------------------------')104 print('下载结束!')105 106 choose=input("请选择下载模式\n1为根据歌单ID下载列表歌曲\n2为根据歌曲ID下载单首歌曲\n")107 if(choose=="1"):108 search=input("请输入歌单ID:")109 url="https://music.163.com/playList?ID="+search # 歌单地址110 music_List=get_List(url) # 获取歌单列表111 mkdir(music_List[0]) # 创建文件夹112 download(music_List) # 通过歌单列表下载音乐113 elif(choose=="2"):114 search=input("请输入歌曲ID:")115 url="https://music.163.com/song?ID="+search # 歌曲地址116 download_song(search,url)117 else:118 print("输入错误!")
cookie与user-agent获取方法:
登录后进入https://music.163.com/
找到后写入代码中即可
总结以上是内存溢出为你收集整理的Python爬取网易云音乐歌单v1.1全部内容,希望文章能够帮你解决Python爬取网易云音乐歌单v1.1所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)