网易云再度升级!用Python爬取下载(二:实现的源代码)

    科技2024-10-13  17

    那么我就不多说了,直接上代码(别人家都是给断断续续的代码然后要下载就得交钱的) 首先是下载歌曲的实现:

    #--*encoding:utf-8*-- import contextlib import requests import json import os import time from requests_html import HTMLSession import pyperclip import eyed3 download = 1 name = 1 by = 1 pic = 1 # import mp3play def q(): os.system('cls') def write(abc, path): paths = path+".lrc" with open(paths,"w") as f: f.write(abc) def get_download_url(id): url = "https://api.imjad.cn/cloudmusic/?type=song&id="+str(id)+ "&br=128000" print("获取歌曲下载地址中……") global download session = HTMLSession() r = session.get(url) r_code = r.html.html start = r_code.find('''"url":"''') start = start + 7 end = r_code.find('''","br"''') download = r_code[start:end] download = download.replace("\/","/") print("获取成功:\n",download,"\n\n") def get_name(id): print("获取歌曲信息中(歌名、艺术家、封面)……\n") global name global by global pic url = "https://api.imjad.cn/cloudmusic/?type=detail&id="+str(id) session = HTMLSession() _g = session.get(url) g = _g.html.html print("\n") name_start = g.find('''"name":"''') name_start += 8 name_end = g.find('''","id"''') name = g[name_start:name_end] by_start = g.find(''',"name":"''') by_start += 9 by_end = g.find('''","tns":''') by = g[by_start:by_end] pic_start = g.find('''picUrl":"''') pic_start += 9 pic_end = g.find(".jpg") pic_end += 4 pic = g[pic_start:pic_end] print("获取成功:\n歌名:",name,"\n艺术家:",by,"\n封面下载地址:",pic,"\n\n") def DownLoad(id): get_name(id) get_download_url(id) global download global name global by global pic ####################################### 歌曲的下载 m = input("是否帮你下载歌曲?") if m == "是": print("正在下载……") filename = name+".mp3" url = download start_time = time.time() response = requests.get(url) with open(filename, 'wb')as f: f.write(response.content) print("下载成功!") fsize = os.path.getsize(filename) fsize = fsize/float(1024 * 1024) audiofile = eyed3.load(filename) audiofile.tag.artist = by end_time = time.time() use_time = str(end_time - start_time) print("本次下载共",fsize,"Mb") print("共用时",use_time,"秒") print() else: x = input("您是否要把下载地址复制到粘贴板?请选择:\n1、打印但不复制\n2、打印且复制\n") if str(x) == "2": pyperclip.copy(download) print(download,"\n已经帮您把音乐下载地址复制到粘贴板,您可以自行下载") else: print("下载地址:\n",download) print("\n\n") ####################################### 封面的下载 i = input("请问是否帮您下载音乐封面?") if i == "是": print("正在下载……") url = pic start_time = time.time() response = requests.get(url) file_name = name+".jpg" file_name = "..//搞网易云心态//音乐封面//"+file_name#####这是我的下载目录,你自己按着自己的下载目录来调 with open(file_name, 'wb')as f: f.write(response.content) print("下载封面到“音乐封面”目录下成功!") fsize = os.path.getsize(file_name) fsize = fsize/float(1024 * 1024) end_time = time.time() use_time = str(end_time - start_time) print("本次下载共",fsize,"Mb") print("共用时",use_time,"秒\n\n") else: j = pyperclip.paste() if j == download: print("您的粘贴板已经有了歌曲的下载链接:\n",j) xy = input("请您选择:\n1、打印但不复制歌曲封面链接\n2、打印且复制歌封面下载链接。\n") if xy == "2": pyperclip.copy(pic) print(pic,"\n已经帮您把音乐封面下载地址复制到粘贴板,您可以自行下载") else: print("封面下载地址:\n",pic) else: pyperclip.copy(pic) print("好的。已经帮您把封面下载地址复制到粘贴板,您可以自行下载") print("\n\n") ####################################### 歌词的下载 print("正在获取歌词中……") url = "https://api.imjad.cn/cloudmusic/?type=lyric&id="+str(id)+"&br=128000" session = HTMLSession() geci_ = session.get(url) a = geci_.text try: geci = a.encode('latin-1').decode('unicode_escape') except: geci = a start = geci.find("[00:") end = geci.find('''"},"klyric":''') end = end -1 geci = geci[start:end] geci = geci.replace("0]","]") geci = str("[00:00.00] 网易云歌曲id:")+str(id)+str("\n")+str(geci) print("歌词如下:\n",geci) print() c = input("是否下载歌词?") if c == "是": write(geci, name) print("已经帮您保存到",name,".lrc了!") else: print("不保存,好的") input("输入回车继续操作……\n\n") ######################################### if __name__ == "__main__": list_paste = [] while True: paste = pyperclip.paste() if "song?id=" in paste: if paste in list_paste: id = input("输入歌曲网址: ") else: print("从您的剪贴板中获取到歌曲网址:",paste) list_paste.append(paste) id = paste else: id = input("输入歌曲网址: ") #http://music.163.com/song?id=1463165983&userid=3262694145 #http://music.163.com/song?id=2061452&userid=2079905574 #https://music.163.com/#/song?id=1471070714 #http://music.163.com/song?id=20707713&userid=3262694145 start = id.find("song?id=") start += 8 if "&us" in id: end = id.find("&userid=") else: end = len(id) if str(start) == "7": print("您输入的网址有误……\n") else: id = id[start:end] print("获得ID:",id,"\n") DownLoad(id)

    程序实现:

    中途的报错在正式运行中是不会出现的 从一个音乐id,就可以帮您获取那么多信息,还免费下载,值了! ##################################我是分段符号################################## 然后就是MV的下载了: 源代码:

    import contextlib import requests import json import os import time from requests_html import HTMLSession import pyperclip import eyed3 mv_name = 1 mv_by = 1 cover = 1 mv_download = 1 def get_imformation(r_code): print('获取mv基础信息中……\n') global mv_name #这段程序为了将后面的有用信息筛选出来,只能取一个删掉前面的,然后继续往后取…… long = len(r_code) print(r_code,"\n") a = r_code.find('''"name":"''') a += 8 code = r_code[a:long] b = code.find('''"''') b += a mv_name = r_code[a:b] code = r_code[b:long] ########### global mv_by c = r_code.find('''artistName":"''') c += 13 code = r_code[c:long] d = code.find('''"''') d += c mv_by = r_code[c:d] code = r_code[d:long] ########### global cover e = r_code.find('''cover":"''') e += 8 f = r_code.find(".jpg") f += 4 cover = r_code[e:f] code = r_code[f:long] ########### global download g = r_code.find('''"brs":{"''') g += 14 code = r_code[g:long] h = code.find('''"''') h += g download = r_code[g:h] print("MV名:",mv_name,"\nMV来自:",mv_by,"\nMV封面下载地址:",cover,"\nMV下载地址:",download) def download_mv(id): url = "https://api.imjad.cn/cloudmusic/?type=mv&id="+str(id)+"&br=128000" session = HTMLSession() r = session.get(url) r_code = r.html.html get_imformation(r_code) global mv_name global mv_by global cover global download print("\n\n") a = input("请问是否需要帮您下载MV封面(虽然我觉得没这个必要)?") if a == "是": print("(这波操作我看不懂……)\n正在下载……") urls = cover start_time = time.time() response = requests.get(urls) file_name = mv_name+"——"+mv_by+".jpg" with open(file_name, 'wb')as f: f.write(response.content) print("下载MV封面成功!") fsize = os.path.getsize(file_name) fsize = fsize/float(1024 * 1024) end_time = time.time() use_time = str(end_time - start_time) print("本次下载共",fsize,"Mb") print("共用时",use_time,"秒\n\n") else: b = input("那请问要帮您把封面下载地址复制到剪切板吗?\n①需要\n②展示但不复制\n") if str(b) == "1": pyperclip.copy(cover) print("已经将",cover,"复制到您的剪切板\n\n") else: print("封面下载地址:/n",cover,"\n\n") c = input("那请问是否帮您下载歌曲MV(肯定是呀)?") if c == "是": print("(猜透你的心思啦!)\n正在下载……") urls = download start_time = time.time() response = requests.get(urls) file_name = mv_name+"——"+mv_by+".mp4" with open(file_name, 'wb')as f: f.write(response.content) print("下载MV成功!") fsize = os.path.getsize(file_name) fsize = fsize/float(1024 * 1024) end_time = time.time() use_time = str(end_time - start_time) print("本次下载共",fsize,"Mb") print("共用时",use_time,"秒\n\n") else: if str(b) == "1": shuniu = "(您的粘贴板已经有封面的下载地址咯)" else: shuniu = "" d = input("那是否要帮您把MV下载地址复制到剪切板",shuniu,"?\n①是的\n②展示但不复制\n") if str(d) == "1": pyperclip.copy(download) print("已经将",download,"复制到您的剪切板\n\n") else: print("MV下载地址:\n",download,"\n\n") if __name__ == "__main__": list_paste = [] while True: paste = pyperclip.paste() if "mv?id=" in paste.replace("/",""): if paste in list_paste: id = input("输入歌曲网址: ") else: print("从您的剪贴板中获取到歌曲网址:",paste) list_paste.append(paste) id = paste else: id = input("输入歌曲网址: ") list_paste.append(id) id = id.replace("/","") #https://music.163.com/#/mv?id=409017&userid=******** #https://music.163.com/#/mv?id=409017 start = id.find("mv?id=") start += 6 end = id.find("&us") if str(end) == "-1": print("您一定是从网页版网易云复制的链接!") end = len(id) else: print("您是从网易云客户端复制的链接吧!") if str(start) == "5": print("您输入的网址有误……\n") else: id = id[start:end] print("获得ID:",id,"\n") download_mv(id)

    这段代码因为有我姐姐的介入所以变得更加人性化了,程序截图: 好了,这就是我和网易云音乐的切磋了,感谢阅读,点赞关注赞赏,爱你么么哒! 上一篇:思路:https://blog.csdn.net/jadsgfuhya/article/details/108122169

    Processed: 0.028, SQL: 8