爬取某音乐榜单歌曲

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

四、爬取结果

相关推荐
wm104310 分钟前
代码随想录第十天 栈和队列
开发语言·python
飞Link27 分钟前
PyTorch 核心 API 完全手册:从基础张量到模型部署
人工智能·pytorch·python·深度学习·机器学习
Dxy123931021634 分钟前
Python使用Playwright入门教程:从环境搭建到实战应用
开发语言·python·playwright
墨抒颖 msy.plus42 分钟前
如何构建现代Agent以OpenManus为例
python·ai编程
爆打维c1 小时前
01BFS算法(例题:网格传送门旅游)
c语言·c++·python·算法·leetcode·广度优先
喵手1 小时前
Python爬虫零基础入门【第六章:增量、去重、断点续爬·第3节】幂等去重:同一条数据反复跑也不会重复入库!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·增量、去重、断点续爬·幂等去重
Python毕设指南1 小时前
基于深度学习的旅游推荐系统
python·深度学习·数据分析·django·毕业设计·课程设计
深蓝电商API1 小时前
Selenium多窗口切换与Cookie管理
爬虫·python·selenium·测试工具
小北方城市网1 小时前
Spring Cloud 服务治理实战:构建高可用微服务体系
spring boot·python·rabbitmq·java-rabbitmq·数据库架构
写代码的【黑咖啡】2 小时前
Python中的Statsmodels:统计建模与假设检验
开发语言·python