爬取某音乐榜单歌曲

一、打开网页https://music.163.com/,进入榜单(热歌榜)

二、右键检查、刷新网页,选择元素(点击歌曲名)

三、相关代码

python 复制代码
import requests
#正则表达式模块内置模块
import re
import os

filename = 'music\\'
if not os.path.exists(filename):
    os.mkdir(filename)
#如果想要爬取其他榜单的内容,只需要更改请求URL中的ID
url = 'https://music.163.com/discover/toplist?id=3778678'
#请求头
headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
response = requests.get(url=url,headers=headers)
# print(response.text)
html_data = re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a>',response.text)

for num_id,title in html_data:
    music_url = f'http://music.163.com/song/media/outer/url?id={num_id}.mp3'
    #对于音乐播放地址发送请求,获取二进制数据内容
    music_content = requests.get(url=url,headers=headers).content
    with open(filename + title +'.mp3',mode='wb') as f:
        f.write(music_content)
    print(num_id,title)

四、爬取结果

相关推荐
赵谨言22 分钟前
Python串口的三相交流电机控制系统研究
大数据·开发语言·经验分享·python
鹿角片ljp1 小时前
Engram 论文精读:用条件记忆模块重塑稀疏大模型
python·自然语言处理·nlp
Blossom.1181 小时前
AI Agent的长期记忆革命:基于向量遗忘曲线的动态压缩系统
运维·人工智能·python·深度学习·自动化·prompt·知识图谱
love530love1 小时前
ComfyUI Hunyuan-3D-2 插件安装问题解决方案
人工智能·windows·python·3d·comfyui·hunyuan-3d-2·pygit2
我是一只小青蛙8882 小时前
Python Pandas 从入门到精通全指南
python
fie88892 小时前
MATLAB有限元框架程序
python·算法·matlab
我是小疯子663 小时前
Python3.11.4离线安装PyInstaller全攻略
python
alphaTao3 小时前
LeetCode 每日一题 2026/1/12-2026/1/18
python·算法·leetcode
Fasda123453 小时前
基于yolo11-C3k2-AKConv的太阳能电池片缺陷检测技术分享
python
观测云3 小时前
AWS Lambda Python 应用可观测最佳实践(DDTrace)
python·云计算·aws