爬取某音乐榜单歌曲

一、打开网页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)

四、爬取结果

相关推荐
大游小游之老游13 小时前
Python中如何实现一个程序运行时,调用另一文件中的函数
python
mantch13 小时前
个人 LLM 接口服务项目:一个简洁的 AI 入口
人工智能·python·llm
weixin_4450547213 小时前
力扣热题51
c++·python·算法·leetcode
朱朱没烦恼yeye13 小时前
java基础学习
java·python·学习
databook14 小时前
数据可视化五大黄金原则:让你的图表“会说话”
python·数据分析·数据可视化
ai_top_trends14 小时前
2026 年度工作计划 PPT 模板与 AI 生成方法详解
人工智能·python·powerpoint
智航GIS14 小时前
9.4 Word 自动化
python·自动化·word
2501_9418091414 小时前
面向多活架构与数据地域隔离的互联网系统设计思考与多语言工程实现实践分享记录
java·开发语言·python
aloha_78915 小时前
agent智能体学习(尚硅谷,小智医疗)
人工智能·spring boot·python·学习·java-ee
亿牛云爬虫专家16 小时前
Worker越简单,系统越稳定:从单机到集群
爬虫·python·集群·爬虫代理·单机·代理ip·worker