编译器环境:Python3.6
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/usr/bin/env python
#-*-coding=utf-8 -*-
#AUTHOR:duwentao
import
requests
import
re
import
json
def
get_sids_by_name(name):
url
=
'http://music.baidu.com/search'
data
=
{
'key'
:name
}
reponse
=
requests.get(url,params
=
data)
reponse.encoding
=
'utf-8'
html
=
reponse.text
#print (html)
ul
=
re.findall(r
'<ul.*</ul>'
, html, re.S)[
0
]
#print(ul)
# 获取sid sid":551560464
sids
=
re.findall(r
'sid":(\d+),'
, ul, re.S)
return
sids
def
get_mp3_by_id(song_id):
song_id
=
song_id
api
=
'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&format=jsonp&callback=jQuery17205500581185420972_1513324047403&songid=%s&_=1513324048127'
%
song_id
response
=
requests.get(api)
data
=
response.text
data
=
re.findall(r
'\((.*)\)'
, data)[
0
]
data
=
json.loads(data)
title
=
data[
'songinfo'
][
'title'
]
mp3_url
=
data[
'bitrate'
][
'show_link'
]
mp3_data
=
requests.get(mp3_url).content
with
open
(
'%s.mp3'
%
title,
'wb'
) as f:
f.write(mp3_data)
print
(
"下载完成"
)
f.close()
sid
=
input
(
"请输入歌手的名字:"
)
sids
=
get_sids_by_name(sid)
for
si
in
sids:
print
(
"正在下载sid为"
,si)
get_mp3_by_id(si)
|
执行结果:
微信公众号:
本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/2053775