9.18 微信小程序开发笔记

如何获取英语单词的发音,使其能在小程序界面通过点击外发?

1.通过外界API获取(例如有道API)

不下载音频文件,每次需要时直接API获取发音,存储压力小。但是一般的API都有使用次数限制,在背单词这种发音请求次数高的情况下,估计次数很快就会用完。可能速度也比较慢。

2.批量下载单词发音音频文件到本地,数据库中发音字段提供音频文件路径

【墨墨英语单词库免费开源无偿分享】小学、初中、高中、大学四六级专四专八、考研、托福、雅思等词书文本大合集_墨墨背单词api-CSDN博客

借助上述资源,获得了CET6.txt文件(6000+行,每行一个单词)

更具以下代码,借助有道API,获取了CET6.txt文件中每个单词的MP3音频文件,存在audio文件夹中

python 复制代码
import requests
import os

# 读取单词列表
def read_words(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        words = [line.strip() for line in file if line.strip()]
    return words

# 下载发音音频
def download_audio(word):
    url = f"http://dict.youdao.com/dictvoice?type=1&audio={word}"
    response = requests.get(url)
    
    if response.status_code == 200:
        # 创建文件夹以保存音频文件
        if not os.path.exists('audio'):
            os.makedirs('audio')
        
        # 保存为MP3文件
        file_path = os.path.join('audio', f"{word}.mp3")
        with open(file_path, 'wb') as audio_file:
            audio_file.write(response.content)
        print(f"Downloaded: {file_path}")
    else:
        print(f"Failed to download audio for: {word}")

# 主程序
def main():
    words = read_words('D:\WeChatCourse\WordsPronunciation\CET6.txt')
    for word in words:
        download_audio(word)

if __name__ == "__main__":
    main()
相关推荐
蒸蒸yyyyzwd3 小时前
day3学习笔记
笔记·学习
小陈phd5 小时前
系统架构师学习笔记(三)——计算机体系结构之存储系统
笔记·学习·系统架构
feasibility.6 小时前
SSH Agent Forwarding 与 tmux 排障笔记
linux·运维·服务器·经验分享·笔记·ssh
dulu~dulu8 小时前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
W起名有点难8 小时前
【Salesforce学习】创建Object笔记
笔记
heartzZ1yy9 小时前
PolarCTF靶场 Crypto 简单 (上)
经验分享·笔记
xw-busy-code9 小时前
Prettier 学习笔记
javascript·笔记·学习·prettier
半壶清水9 小时前
[软考网规考点笔记]-局域网之HDLC 协议
网络·笔记·网络协议·考试
酸奶乳酪9 小时前
IIC学习笔记
笔记·单片机·学习
小陈phd10 小时前
系统架构师学习笔记(二)——计算机体系结构之指令系统
笔记·学习·系统架构