新的一年开始了,过年的时候用酷狗搜索了一些歌单,有一个歌单里的歌特别好听,想下载到MP3里听听,这样也就不用联网了。写个爬虫吧。
1、打开电脑上的酷狗,找到相应的歌单点击分享
2、选择分享到微博
3、分享的时候会显示一个链接
4、好了,有了这个链接就好办了,直接上代码
import requests import re import time #存放地址 filepath = u"f:\日文歌\" #链接地址 url = "http://www.kugou.com/share/7avnC6auGV2.html?id=7avnC6auGV2#hash=6708E2C5707DC29CF13828542C6142A2&album_id=1764069" header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36'} def downmusic(hashstr,musicname): musicurl = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash='+hashstr json = requests.get(musicurl,headers=header).text playurls = re.findall(r'play_url":"(.*?)"',json,re.S) playurl = playurls[0].replace("\","") if playurl!="": r = requests.get(playurl) with open(filepath+musicname+".mp3", "wb") as fp: fp.write(r.content) print(musicname+" 已下载!!!") if __name__ == '__main__': html = requests.get(url,headers=header) htmlstr = html.content htmlstr = htmlstr.decode("unicode-escape", errors='ignore') jsons = re.findall(r'var dataFromSmarty(.*?)]',htmlstr,re.S) if len(jsons)==1: musics = re.findall(r'{"hash":"(.*?)".*?"audio_name":"(.*?)",.*?}',jsons[0],re.S) for music in musics:
#每次休息5秒 time.sleep(5) downmusic(music[0],music[1]) print(" 下载结束!!!")
但是每次启动的时候都会显示,但是下载一样可以下载,感觉是编码问题,哪位大神会的帮忙看一下。。。
DeprecationWarning: invalid escape sequence 'd' htmlstr = htmlstr.decode("unicode-escape", errors='ignore')
pip install pypiwin32
https://download.microsoft.com/download/5/f/7/5f7acaeb-8363-451f-9425-68a90f98b238/visualcppbuildtools_full.exe