Python爬虫实战:网易云音乐爬取
前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 。
本次目标爬取网易云音乐
文章插图
环境
- python 3.6
- pycharm
import requestsimport re
请求网站、解析网站数据def get_music_url(music_id, music_title):url = ''headers = {'Accept': '*/*','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'no-cache','Connection': 'keep-alive','Cookie': 'UM_distinctid=175aca5b31d39e-06d658eceb014a-3962420d-1fa400-175aca5b31e92e','Host': 'api.zhuolin.wang','Pragma': 'no-cache','Referer': '','Sec-Fetch-Dest': 'script','Sec-Fetch-Mode': 'no-cors','Sec-Fetch-Site': 'same-site','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',}params = {'callback': 'jQuery111305698848623906863_1604919341715','types': 'url','id': '{}'.format(music_id),'source': 'netease','_': '1604919341751',}response = requests.get(url=url, params=params, headers=headers)html_data = http://kandian.youth.cn/index/response.textif music_url =='':print('无音频下载链接')def music_id():url = 'discover/toplist'headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}response = requests.get(url=url, headers=headers)lis = re.findall('(.*?) ', response.text, re.S)[0:100]for i in lis:music_id = i[0].split('id=')[-1]title = i[1]pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]")# '/ \ : * ? " < > |'music_title = re.sub(pattern, "_", title)# 替换为下划线get_music_url(music_id, music_title)
保存数据【Python爬虫实战:网易云音乐爬取】
else:path = '保存地址\\' + music_title + '.mp3'response = requests.get(url=music_url)with open(path, mode='wb') as f:f.write(response.content)print(music_title, music_url)
运行代码 , 结果如下图文章插图
文章插图
- 告诉|阿里大佬告诉你如何一分钟利用Python在家告别会员看电影
- Python源码阅读-基础1
- Wireshark数据包分析实战:TCP报文段重组
- Python调用时使用*和**
- 如何基于Python实现自动化控制鼠标和键盘操作
- 解决多版本的python冲突问题
- 学习python第二弹
- Python中文速查表-Pandas 基础
- 零基础小白Python入门必看:通俗易懂,搞定深浅拷贝
- Python 使用摄像头监测心率!这么强吗?