Python爬取实战练习:批量爬取酷我音乐歌曲

前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理
本次目标酷我音乐歌曲爬取

Python爬取实战练习:批量爬取酷我音乐歌曲文章插图
环境介绍

  • python 3.6
  • pycharm
  • requests

Python爬取实战练习:批量爬取酷我音乐歌曲文章插图
开始代码【Python爬取实战练习:批量爬取酷我音乐歌曲】导入工具
import requestsimport re请求网页
headers = {'authority': 'wwwapi.kugou.com','cookie': 'kg_mid=ac3836df72c523f46a85d8a5fd90fe59; kg_dfid=3ve7aQ2XyGmN0yE3uv3WcaHs; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1600260110,1602312707; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; kg_mid_temp=ac3836df72c523f46a85d8a5fd90fe59; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1602312738','referer': 'song/','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',}url = 'yy/rank/home/1-8888.html?from=rank'response = requests.get(url=url, headers=headers)解析网页数据
def func(url):hashs = re.findall('"Hash":"(.*?)"', response.text, re.S)album_ids = re.findall('"album_id":(.*?),"', response.text, re.S)FileNames = re.findall('"FileName":"(.*?)"', response.text, re.S)data = http://kandian.youth.cn/index/zip(hashs, album_ids, FileNames)for i in data:hash = i[0]album_ids = i[1]FileName = i[2].encode('utf-8').decode('unicode_escape')# print(hash, album_ids, FileName)download_url = ''params = {'r': 'play/getdata','callback': 'jQuery19107150201841602037_1602314563329','hash': '{}'.format(hash),'album_id': '{}'.format(album_ids),'dfid': '3ve7aQ2XyGmN0yE3uv3WcaHs','mid': 'ac3836df72c523f46a85d8a5fd90fe59','platid': '4','_': '1602312793005',}保存数据
def download(url, title):filename = 'C:\\Users\\Administrator\\Desktop\\新建文件夹\\' + title + '.mp3'response = requests.get(url=url, headers=headers)with open(filename, mode='wb') as f:f.write(response.content)print(title)运行代码 , 效果如下图
Python爬取实战练习:批量爬取酷我音乐歌曲文章插图
Python爬取实战练习:批量爬取酷我音乐歌曲文章插图


    推荐阅读