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

相关推荐
咕白m62515 小时前
通过 Python 在 PDF 中添加页面
python
wa的一声哭了15 小时前
Linux服务器配置ssh免密登陆多台服务器、服务器别名配置
linux·运维·服务器·网络·arm开发·python·ssh
咖啡の猫16 小时前
Python中的输出函数
开发语言·数据库·python
朱一头zcy16 小时前
下载安装pycharm 并通过pycahrm来驱动Anaconda来运行Python程序
ide·python·pycharm
CodeLongBear17 小时前
Python数据分析 -- Pandas基础入门学习笔记:从核心概念到实操代码
python·conda·pandas
B站_计算机毕业设计之家17 小时前
python手写数字识别计分系统+CNN模型+YOLOv5模型 深度学习 计算机毕业设计(建议收藏)✅
python·深度学习·yolo·计算机视觉·数据分析·cnn
咖啡の猫17 小时前
二进制与字符编码
python
Tech_Lin18 小时前
手搓工具之手写签字识别提取工具
python
Amber_3718 小时前
php的数组和python的列表 -- 横向对比学习
python·学习·php
中文Python18 小时前
小白中文Python-双色球LSTM模型出号程序
开发语言·人工智能·python·lstm·中文python·小白学python