python爬虫下载mp3

import requests
import os

# 基础URL
base_url = "https://www.gushiciqu.com/uploads/mp3/chenlang/"

# 文件名范围
start_num = 49437
end_num = 99999

# 保存目录
save_dir = "E:/项目存档/爬虫/古诗词/"

# 创建保存目录,如果不存在
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 遍历文件名并下载
for num in range(start_num, end_num + 1):
    file_name = f"{num}.mp3"
    full_url = base_url + file_name
    save_path = os.path.join(save_dir, file_name)
    
    try:
        response = requests.get(full_url)
        if response.status_code == 200:  # 成功响应
            with open(save_path, 'wb') as f:
                f.write(response.content)
            print(f"Downloaded: {file_name}")
        else:
            print(f"Failed to download: {file_name} (Status code: {response.status_code})")
    except Exception as e:
        print(f"Error downloading {file_name}: {str(e)}")

print("Download process completed.")