使用有道api抓取读音
转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/106843484 谢谢
使用有道api抓取读音
- pom
- yml
- 工具类
pom
<!-- file转换文件流--><dependency><groupId>org.springframework</groupId><artifactId>spring-mock</artifactId><version>2.0.8</version></dependency>
yml
youdao:downPath: D:/temp/down
工具类
/*** 抓取读音并上传文件服务器工具类** @author LHL*/
@Slf4j
@Component
public class WordVoiceUtil {@Value("${youdao.downPath}")private String downPath;/*** 抓取音频并上传oss** @param content 要抓取读音的内容* @return String*/public String getVoiceLink(String content) {String query = content.toLowerCase().trim();try {query = URLEncoder.encode(query,"utf-8");} catch (UnsupportedEncodingException e) {log.info("抓取读音 \t 内容encode失败: {}", query);e.printStackTrace();}// 0美 1英String speechUrl = "https://dict.youdao.com/dictvoice?type=1&audio=" + query;log.info("抓取读音 \t 抓取地址: {}", speechUrl);String fileUpload = null;File dir = new File(downPath);//创建文件夹if (!dir.exists()) {dir.mkdir();}String path = downPath + "/" + StrUtil.createFileNameUseTime() + ".mp3";log.info("抓取读音 \t 临时下载本地地址: {}", path);URL url = null;InputStream is = null;OutputStream os = null;FileInputStream fileInputStream = null;File audio = null;try {url = new URL(speechUrl);is = new BufferedInputStream(url.openStream());os = new FileOutputStream(path);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {//buffer数组存放读取的字节,如果因为流位于文件末尾而没有可用的字节,则返回值-1,以整数形式返回实际读取的字节数os.write(buffer, 0, bytesRead);}audio = new File(path);if (audio.exists()) {log.info("抓取读音 \t 临时下载本地,下载成功");fileInputStream = new FileInputStream(audio);MultipartFile multipartFile = new MockMultipartFile(audio.getName(), audio.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);fileUpload = FileUploadOssUtil.fileUpload(multipartFile); //上传到文件服务器 看自己的项目用的什么log.info("抓取读音 \t 上传oss文件路径: {}", fileUpload);}} catch (IOException e) {e.printStackTrace();log.error("抓取读音 \t 下载失败");} finally {if (os != null) {try {os.flush();os.close();} catch (IOException e) {e.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if (null != audio) {if (audio.exists()) {audio.delete();log.info("抓取读音 \t 删除临时下载到本地的文件: {}", path);}}}return fileUpload;}}
使用有道api抓取读音相关推荐
- pyhon使用CDS API抓取哥白尼气候数据(详细步骤)
python使用CDS API抓取哥白尼气候数据 [官网:climate data store (cds) - Copernicus Climate Data Store](https://cds.c ...
- 百度音乐API抓取-Fiddler
百度音乐API抓取 前段时间做了一个本地音乐的播放器 github地址,想实现在线播放的功能,于是到处寻找API,很遗憾,不是歌曲不全就是质量不高.在网上发现这么一个APIMRASONG博客,有&qu ...
- 利用百度地图api抓取POI点(上海公交站)
1.功能描述 百度地图api抓取POI点(上海公交站) 2.代码 import pandas as pd import requests import json import numpy as npi ...
- java爬虫抓取行政区划_7-爬虫爬API抓取行政区划(urllib).ipynb
{ "cells": [ { "cell_type": "markdown", "metadata": {}, &quo ...
- 教你用百度地图API抓取建筑物周边位置、房价信息(附代码)
来源:大数据挖掘DT数据分析 本文共2465字,建议阅读5分钟. 本文为大家解读怎样用脚本与百度API的交互,爬取百度地图的数据. 需求描述 对于数据挖掘工程师来说,有时候需要抓取地理位置信息,比如统 ...
- 【Python】 调用百度地图API抓取西安市小区信息
前面有同学参加市场调查大赛,需要西安市的小区信息数据,一个小爬虫程序完美解决. 百度地图开放平台 详情访问:百度PlaceAPI 这里用到了矩形区域检索,具体参数说明如下: 调取详情 所需库: imp ...
- python爬斗鱼直播_从零开始写Python爬虫 --- 爬虫应用: 利用斗鱼Api抓取弹幕
七月末的南京简直开启了「微波炉」模式,白天要学车的我,晚上自然选择宅在家看直播,看着狗贼叔叔满屏幕的弹幕,我就想着能不能把弹幕爬下来呢?说干就干 结果的展示: 这里只抓到弹幕内容和发送用户 并输出在终 ...
- python抓取网页文章_使用Python从公共API抓取新闻和文章
python抓取网页文章 Whether you are data scientist, programmer or AI specialist, you surely can put huge nu ...
- Scrapy 案例一:通过 API 抓取天气预报数据
目录 一.概述 二.案例分析 三.编码实现 四.获取多个 city 的天气信息(简单实现) 一.概述 在一些简单的网站中,可以发现,所有的数据都在网页代码中,然而在实际获取数据的过程中,我们可以发现, ...
最新文章
- 具体数学第二版第四章习题(1)
- poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】
- 如何清理asp站点缓存的代码
- luajit日记-FFI库
- 图像形状特征(八)--SC形状上下文
- P2420 让我们异或吧 (树链剖分,异或前缀和)
- CCF推荐会议与期刊(人工智能 CV)
- 第三方对接秀米图文编辑器遇到的坑
- 常见的浏览器以及内核
- 零基础学CocosCreator·第八季-双人对战五子棋
- 【推荐系统论文精读系列】(十二)--Neural Factorization Machines for Sparse Predictive Analytics
- css b加粗怎么用,CSS去掉b加粗和strong加粗标签样式.doc
- 树莓派3B+安装ubuntu16.04与ROS
- android wear 2.0 访问网络,离线AI使Android Wear 2.0断网也智能回复
- 【问题篇】Springboot解决报错“java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_passw”
- 一种新型的智能城市工业物联网安全攻击检测框架
- adb连接夜深模拟器
- Arduino提高篇07—超声波测距
- 英语入门造句专用名词表(共 396 个)
- VMWare------启动虚拟机时出现“start booting fron CD... Error loading image:DFEAULT.EZB”提示...