Compiler environment: Python3.6
Code:
#!/usr/bin/env python#-*-coding=utf-8 -*-#AUTHOR:d uwentaoimport requestsimport Reimport jsondef 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) # get sid sid":551560464 sids = re.findall (R ' sid":( \d+), ', ul, re. S) return sidsdef 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 ("Download Complete") f.close () sid = input ("Please enter the singer's name:") SIDS&NBsp;= get_sids_by_name (SID) for si in sids: print ("Downloading SID is", Si) get_mp3_by_id (SI)
Execution Result:
Public Number:
Python crawls to Baidu Music