python爬取酷狗音乐免费歌曲

作者为初学者,此代码仅供学习交流,切不可违法、商用。

import re
import requests
import json
print('请输入搜索歌曲:', end='')
search = input() #搜索内容
pagesize = '1' #请求数目
base_url1='https://songsearch.kugou.com/song_search_v2?callback=jQuery112406640696046150798_1568961432710&keyword=%s&page=1&pagesize=%s&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1568961432712' % (search,pagesize)
target1 = requests.get(base_url1)
FileHash=json.loads(re.match(".*?({.*}).*", target1.text, re.S).group(1)).get("data").get("lists")[0].get("FileHash")
AlbumID=json.loads(re.match(".*?({.*}).*", target1.text, re.S).group(1)).get("data").get("lists")[0].get("AlbumID")
base_url2='https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery1910016186010350987656_1568878471243&hash=%s&album_id=%s&dfid=4DfdMz14MELP0Y5jh20Hzjg2&mid=dd8432bb2d3eee37ed8bea39ab8cfbdb&platid=4&_=1568878471244' %(FileHash,AlbumID)
print(base_url2)
target2=requests.get(base_url2)
link=json.loads(re.match(".*?({.*}).*", target2.text, re.S).group(1)).get("data").get("play_url")
html=requests.get(link)
with open('E:/pyMusic/'+search+'.mp3','wb') as f :
    f.write(html.content)

你可能感兴趣的:(python爬取酷狗音乐免费歌曲)