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()
相关推荐
xw-busy-code16 小时前
sass学习笔记整理
笔记·学习·sass
xuhaoyu_cpp_java1 天前
过滤器与监听器学习
java·经验分享·笔记·学习
LegendNoTitle1 天前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
Oll Correct1 天前
实验八:验证以太网交换机的生成树协议STP
网络·笔记
_李小白1 天前
【OSG学习笔记】Day 2: Hello Cow
笔记·学习
鹭天1 天前
Langchain学习笔记
笔记·学习·langchain
GLDbalala1 天前
GPU PRO 5 - 1.2 Reducing Texture Memory Usage by 2-Channel Color Encoding 笔记
笔记
IT19951 天前
Docker笔记-对docker-compose.yml基本认识
笔记·docker·容器
猹叉叉(学习版)1 天前
【系统分析师_知识点整理】 1.计算机系统
笔记·软考·系统分析师