编译器环境:Python3.6
代码:
- #!/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)
执行结果:
微信公众号:
原文:http://blog.51cto.com/tdcqvip/2053775
来源: http://www.bubuko.com/infodetail-2437599.html