1. 程式人生 > >Requests 、re、json庫解析下載,百度千千音樂檔案

Requests 、re、json庫解析下載,百度千千音樂檔案


剛步入Python學習它,記錄自己的軌跡,加油學習。




 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import requests
 5 import re
 6 import json
 7 
 8 
 9 class GETQQMP3:
10 
11     def __init__(self):
12         pass
13 
14     def getSID(self,key,slt):
15 
16         url = 'http://music.taihe.com/search?key='
+key 17 res = requests.get(url).text 18 data = re.findall(r'data-playdata="(.+?)"',res) 19 for i in data: 20 data_ID = re.findall(r'\d+',i) 21 slt.append(data_ID) 22 23 def get_Mp3(self,cid): 24 for i in cid[0]: 25 mp3_Url = 'http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&callback=jQuery17202628249451505169_1542286502046&songid=%s&from=web&_=1542286504081
'%i #print(mp3_Url) 26 res = requests.get(mp3_Url).text 27 response = re.findall(r'\((.*)\)',res)[0] 28 data = json.loads(response) 29 mp3_title = data['songinfo']['title'] 30 mp3_path = data['bitrate']['file_link'] 31 32 self.saveMp3(mp3_title,mp3_path)
33 #print(mp3_title,mp3_path) 34 35 36 def saveMp3(self,mp3_title,mp3_url): 37 mp3 = requests.get(mp3_url) 38 with open(r'C:\Users\Administrator\Desktop\MP3\%s.mp3' % mp3_title, 'wb') as f: 39 f.write(mp3.content) 40 41 42 43 if __name__ =='__main__': 44 p = GETQQMP3() 45 slt = [] 46 p.getSID('王心凌',slt) 47 p.get_Mp3(slt)