Pytho爬取音乐

import requests

from bs4 import BeautifulSoup

步骤1: 发送HTTP请求获取网页内容

url = 'https://y.qq.com/n/ryqq/player' # 替换为实际的网页URL

response = requests.get(url)

检查请求是否成功

if response.status_code == 200:

步骤2: 解析HTML内容

soup = BeautifulSoup(response.text, 'html.parser')

假设音乐链接在class为'music-link'的a标签中

music_links = soup.find_all('a', class_='music-link')

步骤3: 遍历音乐链接并下载音乐文件

for link in music_links:

music_url = link.get('href') # 获取音乐链接的URL

music_filename = music_url.split('/')[-1] # 从URL中提取文件名

发送请求下载音乐文件

with requests.get(music_url, stream=True) as r:

with open(music_filename, 'wb') as f:

for chunk in r.iter_content(chunk_size=8192):

f.write(chunk)

print(f'Downloaded {music_filename}')

else:

print('Failed to retrieve the webpage.')

相关推荐
TonyLee0171 小时前
使用argparse模块以及shell脚本
python
Blossom.1181 小时前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
love530love3 小时前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
MediaTea3 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
jarreyer3 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas
代码or搬砖4 小时前
HashMap源码
开发语言·python·哈希算法
顽强卖力5 小时前
第二章:什么是数据分析师?
笔记·python·职场和发展·学习方法
站大爷IP6 小时前
Python实现Excel数据自动化处理:从繁琐操作到智能流程的蜕变
python
BBB努力学习程序设计6 小时前
Python 进阶知识点精讲:上下文管理器(Context Manager)的原理与实战
python·pycharm
清水白石0086 小时前
《深入 super() 的世界:MRO 与 C3 线性化算法的全景解析与实战指南》
python