Simulation browser, access to Internet resources, according to the rules written, download the required music data!
Source Code view: Developer tools in more tools (Shift+ctrl+i or F12)
Login to http://music.taihe.com music in Google Chrome
Write code in Pycharm
In terminal, enter PIP install requests Enter to install requests
Program code:
Import requests
Import re
#第一步 Get Song IDs
Search_api = 'Http://music.taihe.com/search‘
#搜索的关键字, passing parameters, constructed from a dictionary
Keyword = {' key ': 'Alan‘}
#发送请求
#get请求 params is a pass-through get parameter
Response=requests.get (Search_api,params=keyword)
#取出html的源码
response.encoding= ' Utf-8 '
HTML = Response.text
#通过正则表达式获取id
Ids=re.findall (R '{"id":" (\d+) "', HTML)
Print (IDS)
#第二步 get information about a song
Mp3_info_api = 'Http://play.taihe.com/data/music/songlink‘
Data ={
' songids ': ', '. Join (IDS),
' HQ ': 0,
' type ': ' M4a,mp3 ',
' rate ': ',
' PT ': 0,
' flag ':-1,
' s2p ':-1,
' prerate ':-1,
' BWT ':-1,
' dur ':-1,
' bat ':-1,
' BP ':-1,
' pos ':-1,
' auto ':-1
}
#data就是post的参数
res = Requests.post (mp3_info_api,data=data)
#返回的数据是json格式 call the JSON method directly and turn it into a dictionary
info = Res.json ()
#第三步 to download the song
#根据数据的结构 to get information about a song
Song_info =info[' data ' [' songList ']
#循环
For song in Song_info:
#根据数据结构获取信息
#歌名
Song_name = song[' Songname ']
#mp3地址
Song_link = song[' Songlink ']
#格式
For_mat = song[' format ']
#歌词地址
Lrclink = song[' Lrclink ']
# Print (Song_name,song_link,for_mat,lrclink)
#下载mp3
If Song_link: #有可能没有地址
Song_res =requests.get (Song_link)
With open ('%s.%s '% (song_name,for_mat), ' WB ') as F:
F.write (song_res.content)
Print (Song_link)
#下载歌词
If Lrclink:
Lrc_response = Requests.get (Lrclink)
#写文件
With open ('%S,LRC '% song_name, ' W ') as F:
F.write (Lrc_response.text)
Python Climb Chin Music