Python爬去百度音乐

编译器环境: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&quot;:551560464
     sids  =  re.findall(r 'sid&quot;:(\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)



执行结果:

1.png

2.png


微信公众号:

qrcode_for_gh_0cd223682950_430.jpg


本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/2053775


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值