这篇文章主要介绍“Python怎么爬取网易云音乐”,在日常操作中,相信很多人在Python怎么爬取网易云音乐问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python怎么爬取网易云音乐”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
爬取网易云音乐
https://music.163.com/
python 3.6
pycharm
导入工具
import requests import re
请求网站、解析网站数据
def get_music_url(music_id, music_title): url = 'https://api.zhuolin.wang/api.php' 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': 'https://music.zhuolin.wang/', '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 = response.text if music_url == '': print('无音频下载链接') def music_id(): url = 'https://music.163.com/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('<li><a href="(.*?)">(.*?)</a></li>', response.text, re.S)[0:100] for i in lis: music_id = i[0].split('id=')[-1] title = i[1] pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # '/ \ : * ? ">
保存数据
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怎么爬取网易云音乐”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。