简易网易云音乐接口开发

简易网易云音乐接口开发

前言

调用网易云音乐官方的接口,用requests获取音乐地址

准备

模块 作用
flask 封装api
requests 解析接口
vue
axios

开始

获取音乐id

抓包获取接口
http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={id}&type=1&offset={//翻页}&total=true&limit=20 
  1. id ==> 搜索信息
  2. type ==> { 1:单曲 ,10:专辑 ,100:歌手,1000:歌单,1004:MV }
  3. offset ==> 翻页,为limit的倍数
  4. limit ==> 请求一次返回的歌曲个数

请求返回信息

{ "result":{ "songs":[ { "id":29723022, //歌曲id "name":"暗里着迷 (Live)", //歌曲名称 "artists":[ { "id":3691, "name":"刘德华", "picUrl":null, "alias":[ ], "albumSize":0, "picId":0, "img1v1Url":"http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg", "img1v1":0, "trans":null } ], "album":{ "id":3066282, "name":"Wonderful World 香港演唱会 2007", "artist":{ "id":0, "name":"", "picUrl":null, "alias":[ ], "albumSize":0, "picId":0, "img1v1Url":"http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg", "img1v1":0, "trans":null }, "publishTime":1201795200007, "size":34, "copyrightId":0, "status":0, "picId":109951165909154050, "mark":0 }, "duration":226000, "copyrightId":0, "status":0, "alias":[ ], "rtype":0, "ftype":0, "mvid":0, "fee":0, "rUrl":null, "mark":0 }, { "id":29723041, "name":"17岁 (Live)", "artists":[ { "id":3691, "name":"刘德华", "picUrl":null, "alias":[ ], "albumSize":0, "picId":0, "img1v1Url":"http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg", "img1v1":0, "trans":null } ], "album":{ "id":3066282, "name":"Wonderful World 香港演唱会 2007", "artist":{ "id":0, "name":"", "picUrl":null, "alias":[ ], "albumSize":0, "picId":0, "img1v1Url":"http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg", "img1v1":0, "trans":null }, "publishTime":1201795200007, "size":34, "copyrightId":0, "status":0, "picId":109951165909154050, "mark":0 }, "duration":303000, "copyrightId":0, "status":0, "alias":[ ], "rtype":0, "ftype":0, "mvid":0, "fee":0, "rUrl":null, "mark":0 } ], "songCount":2369 }, "code":200 } 
根据返回信息获取编写代码
import requests url = "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={}&type=1&offset={}&total=true&limit=20".format(content,offset*10) HtmlTostr = requests.get(url,headers=headers).text HtmlToJson = json.loads(HtmlTostr) musicinfo = HtmlToJson["result"]["songs"] return musicinfo 

此时返回的musicinfo 为搜索的的所有信息 包括歌曲id

然后根据id解析歌曲地址
def GetMusic(self,id): url = "http://music.163.com/song/media/outer/url?id={}.mp3".format(id) headers = requests.head(url, headers= self.headers).headers MusicSrc = headers['Location'] return MusicSrc 

MusicSrc就是歌曲解析出来的真实地址了

将以上两段代码写成一个类供后续调用 wymusic.py

import requests import json class music(): def __init__(self): self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE' } def GetId(self,content,offset=0): """ :return musicinfo 返回歌曲信息: -musicinfo["id"] 歌曲id -musicinfo["name"] 歌曲名称 -musicinfo["fee"] 歌曲是否付费 :info content:搜索内容 offset :翻页 """ url = "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={}&type=1&offset={}&total=true&limit=20".format(content,offset*10) HtmlTostr = requests.get(url,headers=self.headers).text HtmlToJson = json.loads(HtmlTostr) musicinfo = HtmlToJson["result"]["songs"] return musicinfo def GetMusic(self,id): url = "http://music.163.com/song/media/outer/url?id={}.mp3".format(id) headers = requests.head(url, headers= self.headers).headers MusicSrc = headers['Location'] return MusicSrc 

编写Flask主程序

from flask import Flask,render_template,request,redirect import webview from wymusic import music app = Flask(__name__) @app.route("/",methods=["GET","POST"]) def index(): if request.method == "GET": return render_template("index.html") if request.method =="POST": music_message = request.form.get("search_music") musicsrc = music() musicdatas = musicsrc.GetId(music_message) return render_template("index.html",musicdatas = musicdatas) @app.route("/start/<int:num>",methods=["GET","POST"]) def start(num): musics = music() song = musics.GetMusic(num) return song if __name__ == '__main__': app.run() # # window = webview.create_window( # title="cherry", # url= app, # width=960, # height=600, # resizable=False, # text_select=False, # confirm_close=True, # ) # webview.start() 

函数index,提供主页面及逻辑处理,函数start为Vue+axios,提供接口

编写界面

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <link href="../static/css/index.css" rel="stylesheet"> </head> <body> <div id="app"> <form action="/" method="POST"> <input type="text" name="search_music"> <input type="submit" value="提交"> </form> <ul> {% for musicdata in musicdatas %} <li class="startmusic" v-on:click="getmusic({{ musicdata.id }})">{{musicdata.name}}==>{{ musicdata.artists[0].name }}</li> {% endfor %} </ul> <audio autoplay loop controls v-bind:src="musicurl"></audio> </div> </body> <script> var app = new Vue({ el:"#app", data:{ musicurl : "" }, methods:{ getmusic:function (p1) { console.log(p1) axios.get("http://127.0.0.1:5000/start/"+p1 ).then((res)=>{ this.musicurl = res.data },(err)=>{ console.log(err) }) } } }) </script> </html> 

其中script中 getmusic函数为调用的start函数

实现结果

image-20210509104130443

总结

原文链接:https://www.cnblogs.com/sushiyv/p/14747367.html

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/5005

(0)
上一篇 2022年7月30日 22:09
下一篇 2022年7月30日 22:09

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml