这个网页中有多个mp3音频
https://www.
barefootbooks.com/tales
ofmystery
查看源代码,找到mp3文件:
https://
barefootbooks-media-raw.s3.amazonaws.com
/audio/TalesOfMysteryAndMagic/TalesofMysteryandMagic_2_TheBlackbirdandhisWife.mp3
https://barefootbooks-media-raw.s3.amazonaws.com
/audio/TalesOfMysteryAndMagic/TalesofMysteryandMagic_2_TheBlackbirdandhisWife.mp3
" type="audio/mpeg">
Your browser does not support the audio element.
在deepseek中输入提示词:
你是一个Python编程专家,写一个Python脚本,完成任务如下:
打开网页:
https://www.
barefootbooks.com/tales
ofmystery
定位源代码中所有audio标签,然后定位其中的
标签,提取其src属性,这是MP3下载链接;
下载所有的mp3音频,保存到文件夹:F:\aivideo
注意:每一步都要输出信息到屏幕上
要完成这个任务,我们可以使用Python的requests库来下载网页内容,使用BeautifulSoup库来解析HTML并提取所需的音频链接,最后使用requests库来下载这些MP3文件。以下是一个完整的Python脚本:
import os
import requests
from bs4 import BeautifulSoup
# 定义目标网页和保存路径
url = "
https://www.
barefootbooks.com/tales
ofmystery
"
save_folder = r"F:\aivideo"
# 确保保存文件夹存在
if not os.path.exists(save_folder):
os.makedirs(save_folder)
print(f"Created folder: {save_folder}")
# 下载网页内容
print(f"Fetching content from {url}...")
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
# 解析HTML内容
print("Parsing HTML content...")
soup = BeautifulSoup(response.text, 'html.parser')
# 查找所有的
audio_tags = soup.find_all('audio')
print(f"Found {len(audio_tags)}
# 遍历每个
mp3_links = []
for audio in audio_tags:
source_tags = audio.find_all('source')
for source in source_tags:
src = source.get('src')
if src and src.endswith('.mp3'):
mp3_links.append(src)
print(f"Found MP3 link: {src}")
# 下载所有的MP3文件
for mp3_link in mp3_links:
print(f"Downloading {mp3_link}...")
mp3_response = requests.get(mp3_link)
mp3_response.raise_for_status()
# 提取文件名
file_name = os.path.join(save_folder, os.path.basename(mp3_link))
# 保存文件
with open(file_name, 'wb') as f: