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

相关推荐
Sunshine for you12 分钟前
如何用FastAPI构建高性能的现代API
jvm·数据库·python
阿贵---16 分钟前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
Red丶哞1 小时前
内网自建Postfix使用Python发送邮件
开发语言·python
rebekk1 小时前
pytorch custom op的简单介绍
人工智能·pytorch·python
chushiyunen1 小时前
uv使用笔记(python包的管理工具)
笔记·python·uv
曲幽1 小时前
FastAPI状态共享秘籍:别再让中间件、依赖和路由“各自为政”了!
python·fastapi·web·request·state·depends·middleware
风清扬【coder】1 小时前
Anaconda 被误删后抢救手册:数据恢复 + 环境重建应急流程
python·数据恢复·anaconda·环境重建
2401_884563241 小时前
进阶技巧与底层原理
jvm·数据库·python
2401_873204651 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
l1t1 小时前
DeepSeek 辅助编写python程序求解欧拉计划932题:2025数
开发语言·python·欧拉计划