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

相关推荐
笨鸟先飞,勤能补拙11 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例
人工智能·python·安全·web安全·网络安全·sqlite·github
长和信泰光伏储能12 小时前
京津冀光伏发电:绿色能源的未来之路
python·能源
浦信仿真大讲堂12 小时前
从重复操作到自动化闭环:如何让 CST 与 Python 真正协同起来
python·自动化·cst·仿真软件·达索软件
Gu Gu Study13 小时前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)
人工智能·python
卷无止境13 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境13 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
lpfasd12313 小时前
MediaCrawler 项目深度分析
chrome·python·chrome devtools
Dxy123931021614 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
bamb0014 小时前
一个项目带你入门AI应用开发01
python
05664615 小时前
Python康复训练——常用标准库
开发语言·python·学习