爬取某音乐榜单歌曲

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

四、爬取结果

相关推荐
RAY_01047 分钟前
Python—数据可视化pyecharts
开发语言·python
Lvan的前端笔记26 分钟前
python:用 dotenv 管理环境变量&生产环境怎么管理环境变量
网络·数据库·python
Java Fans26 分钟前
用PyQt打造带动画、碰撞检测和键盘控制的小游戏
python·计算机外设·pyqt
深蓝海拓27 分钟前
PySide6从0开始学习的笔记(十一) QSS 属性选择器
笔记·python·qt·学习·pyqt
AAA_bo130 分钟前
liunx安装canda、python、nodejs、git,随后部署私有网页内容提取工具--JinaReader全攻略
linux·python·ubuntu·typescript·aigc·python3.11·jina
高洁011 小时前
DNN案例一步步构建深层神经网络(3)
python·深度学习·算法·机器学习·transformer
AI_56781 小时前
Jupyter交互式数据分析的效率革命
开发语言·python
superman超哥1 小时前
仓颉语言中并发集合的实现深度剖析与高性能实践
开发语言·后端·python·c#·仓颉
superman超哥1 小时前
仓颉语言中原子操作的封装深度剖析与无锁编程实践
c语言·开发语言·后端·python·仓颉
拾贰_C1 小时前
【Anaconda | Python | pytorch】sklearn scikit-learn 报错:
pytorch·python·sklearn