python解析五大音乐平台教程之网易云音乐搜索加直链提取
首先先说明一下,我们这次使用的python版本为python3
,用到的库是requests
,
用到API
- 网易云搜索API
http://music.163.com/api/cloudsearch/pc
- 网易云下载API
http://music.163.com/api/song/enhance/player/url
第一步,搜索需要的歌曲
搜索歌曲的请求方式为post
请求,请求需要的参数为音乐名字,类型,偏移量,音乐搜索的最大个数
当我们搞清楚上面的内容的时候就可以开始写代码了。
import requests
netease_url="http://music.163.com/api/cloudsearch/pc"
参数拼接
params={'s':'客官不可以','type':1,'offset':0,'limit':10}
进行POST请求
r=requests.post(netease_url,data=params)
提取歌曲ID
首先我们输入r.json()
就可以获取搜索返回的json
数据
类似于下面的样子,我们需要从这里面找到歌曲的ID
{
"result":{
"songs":[
{
"name":"客官不可以",
"id":174963,
"pst":0,
"t":0,
"ar":[
{
"id":5929,
"name":"徐良",
"tns":[
],
"alias":[
"L"
],
"alia":[
"L"
]
},
{
"id":10123,
"name":"小凌",
"tns":[
],
"alias":[
]
}
],
"alia":[
],
"pop":100,
"st":0,
"rt":"600902000009094539",
"fee":0,
"v":14,
"crbt":null,
"cf":"",
"al":{
"id":17598,
"name":"犯贱",
"picUrl":"http://p1.music.126.net/ImNPIBQmUdqCjdeAs6mr6A==/28587302323242.jpg",
"tns":[
],
"pic":28587302323242
},
"dt":227422,
"h":{
"br":320000,
"fid":0,
"size":9116276,
"vd":0.568513
},
"m":{
"br":160000,
"fid":0,
"size":4571494,
"vd":1.45
},
"l":{
"br":96000,
"fid":0,
"size":2753163,
"vd":1.25093
},
"a":null,
"cd":"1",
"no":4,
"rtUrl":null,
"ftype":0,
"rtUrls":[
],
"rtype":0,
"rurl":null,
"mst":9,
"cp":22019,
"mv":0,
"publishTime":1293120000000,
"privilege":{
"id":174963,
"fee":0,
"payed":0,
"st":0,
"pl":320000,
"dl":320000,
"sp":7,
"cp":1,
"subp":1,
"cs":false,
"maxbr":128000,
"fl":320000,
"toast":false,
"flag":0
}
},
因为我们解析的歌曲有多个,所以我们用一个列表,来将所有歌曲的信息记录下来
myids=my_json_data['result']['songs']
下载
先写下载的API
netease_download_url="http://music.163.com/api/song/enhance/player/url"
然后,通过for
语言迭代获取id
,拼接下载链接
for myIdJson in myids:
myid=myIdJson['id']
download_params={'ids':'['+str(myid)+']','br':320000}
d = requests.post(netease_download_url, data=download_params)
my_download_data = d.json()
download_url = my_download_data['data'][0]['url']
print(download_url)
因为每次搜索的音乐几乎都有多个,因为歌曲重名,所以,我们需要解析歌手来作为区分的工具
myauthors=myIdJson['ar']
authors=''
for author in myauthors:
authors+=author['name']
最后的解析的结果
小凌http://m10.music.126.net/20171207164659/638c1bb7031f467897238fe5c71c8edc/ymusic/ca16/e354/50ae/7662ca5a1fb946980c94eb89e0b42dab.mp3
徐良http://m10.music.126.net/20171207164659/638c1bb7031f467897238fe5c71c8edc/ymusic/ca16/e354/50ae/7662ca5a1fb946980c94eb89e0b42dab.mp3
放假君http://m10.music.126.net/20171207164659/e71095bbe3227234d75bd05decd16c0f/ymusic/be7a/3300/df7f/1c01caae33813b42ed5086f5f0e45b68.mp3
艾辰http://m10.music.126.net/20171207164659/8906214f3a0645b25a222e94a27c4eff/ymusic/bccd/22e7/7ff8/1e185d6bb3819acdfe72c698c8cd3e93.mp3
徐良http://m10.music.126.net/20171207164700/cb0df7c6b6a8aa94dfc26a6ebdab521f/ymusic/9474/b096/a198/0b3c1a475ff6611c7394668124777edb.mp3
徐良http://m10.music.126.net/20171207164700/38578d0c4f8d62eb9076ed6ca13459a0/ymusic/a66d/22c2/729c/e0952914976273307af836fc959172fd.mp3
OveRcurrenthttp://m10.music.126.net/20171207164700/4b938af7f36ea1f6a020727915f89364/ymusic/a5c1/241a/6b02/20c340a6d8291767eaf7d07fd0079b38.mp3
Mc敏少http://m10.music.126.net/20171207164700/04e051fe0f861578739c0d16b60444ad/ymusic/f98d/0843/1e81/ff8fbe517407b58d898c643be58f8f8f.mp3
Ember Islanderhttp://m10.music.126.net/20171207164700/0b3df5619dfde4fb49aff9a28a865ca1/ymusic/d634/cb31/7051/ff4ea47c7d4099f8f366ebc40186eab4.mp3
E8rhttp://m10.music.126.net/20171207164700/2fbd057c3620d22fba0a4674564ba60b/ymusic/d78c/0917/5aab/7aa69ee5c968639ec4b5d08f80c8a07a.mp3
之后只要我们复制link到浏览器中就可以直接下载了。
新手勿喷