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

相关推荐
用户83562907805120 小时前
Python 设置 Excel 条件格式教程
后端·python·excel
2401_8747325321 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
l1t21 小时前
与系统库同名python脚本文件引起的奇怪错误及其解决
开发语言·数据库·python
Jackey_Song_Odd21 小时前
Part 1:Python语言核心 - 内建数据类型
开发语言·python
带娃的IT创业者21 小时前
WeClaw WebSocket 连接中断诊断:从频繁掉线到稳定长连的优化之路
python·websocket·网络协议·php·fastapi·实时通信
GinoWi21 小时前
Chapter 4 Python中的循环语句和条件语句
python
GinoWi21 小时前
Chapter 5 Python中的元组
python
前进的李工1 天前
LangChain使用之Model IO(提示词模版之PromptTemplate)
开发语言·人工智能·python·langchain
Storynone1 天前
【Day27】LeetCode:56. 合并区间,738. 单调递增的数字
python·算法·leetcode
叶子2024221 天前
承认错误才能成长
python