再也不怕深夜emo的时候没会员了,无敌Python只需20行代码,轻松爬取网易云排行榜热门音乐! 让我们直接开始本次操作吧!
import random import time import execjs import requests import re """发送请求: 模拟浏览器对于url地址发送请求""" # 模拟浏览器 headers = { 'User-Agent':'*******” (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' } # 请求网址: 热歌榜链接 link = 'https://********/discover/toplist?id=3778678' # 获取数据 html = requests.get(url=link, headers=headers).text # 使用正则提取歌曲ID 歌名 info = re.findall('<a href="/song\?id=(\d+)">(.*?)</a>', html) # for循环遍历 for music_id, title in info: # time.sleep(random.randint(1,2)) # 替换特殊字符 new_title = re.sub(r'[\\/:*?"<>|]', '', title) print(music_id, title) # 请求网址 url = 'https://com/weapi/song/enhance/player/url/v1?csrf_token=' # 读取js代码 js_file = open('网易.js', encoding='utf-8').read() # 编译js代码 js_code = execjs.compile(js_file) # 参数 a = { "ids": f"[{music_id}]", "level": "standard", "encodeType": "aac", "csrf_token": "" } # 完整源码加v Python1018 备注{ 圆圆 } 获取 不备注不通过验证哦 # 调用js代码函数 resp = js_code.call('get_data', a) print(resp) # 请求参数 (需要通过调用js代码获取) data = { 'params':resp['encText'], 'encSecKey':resp['encSecKey'], } # 发送请求 response = requests.post(url=url, data=data, headers=headers) """获取响应json数据""" json_data = response.json() """解析数据: 提取歌曲链接""" music_url = json_data['data'][0]['url'] """保存数据: 获取歌曲内容, 进行数据保存""" # 获取歌曲内容 music_content = requests.get(url=music_url, headers=headers).content # 保存数据 with open('music\\' + title + '.mp3', mode='wb') as f: # 写入数据 f.write(music_content) print(music_url)
大家快去试一下吧!!
标签:20,headers,Python,title,js,爬取,url,music,data From: https://www.cnblogs.com/yuanyuan1010/p/18398971