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