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.')

相关推荐
chushiyunen4 分钟前
langchain4j笔记、tools
笔记·python·flask
程序员三藏1 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
在放️1 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
财经资讯数据_灵砚智能2 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月14日
大数据·人工智能·python·ai·信息可视化·自然语言处理·灵砚智能
JOJO数据科学4 小时前
JupyterLab Electron 鸿蒙 PC 适配全记录:从 Python 原生崩溃到 node-static 本地工作台
python·electron·harmonyos
xufengzhu4 小时前
第三方 Python 库 redis-py + hiredis 的使用
开发语言·redis·python
llxxyy卢5 小时前
polar夏季赛部分题目
开发语言·python
闵孚龙5 小时前
PyTorch 系列 之 nn.Module:所有模型的骨架
人工智能·pytorch·python
AI玫瑰助手5 小时前
Python模块:from...import...导入指定内容
开发语言·python·信息可视化
小森林之主5 小时前
Python re 模块速查:从实战对比中掌握正则表达式
python·正则表达式·性能测试·re模块·编程实战