从谷歌和译典通能上自动下载英语单词mp3的好方法

最近在学英语,很多单词不怎么会读.每次得去iciba.com和dreye.com上查,很是麻烦!

后来一想,能不能写个程序自动下载?测试一番,最终搞定!

下载的都是真人语音mp3,效果很棒!谷歌爱词霸是女声发音,译典通是男声发音,各有千秋.

核心的代码:

bool DownMp3FromDrEye(string word, string localPath)
        {
            HttpWebRequest webRequest = WebRequest.Create("http://www.dreye.com.cn/ews/dict.php") as HttpWebRequest;
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

string postString = "w=" + word;
            postString = System.Web.HttpUtility.HtmlEncode(postString);
            byte[] postData = Encoding.ASCII.GetBytes(postString);
            webRequest.ContentLength = postData.Length;

Stream reqStream = webRequest.GetRequestStream();
            reqStream.Write(postData, 0, postData.Length);
            reqStream.Close();

Stream respStream = webRequest.GetResponse().GetResponseStream();
            StreamReader reader = new StreamReader(respStream, true);
            string html = reader.ReadToEnd();
            respStream.Close();
            //Debug.Write(html);

string prefix = "http{add}";
            int i = html.IndexOf(prefix);
            int k = i + prefix.Length;
            string mp3Url = "http:";
            while (html.Substring(k,1)!="\"")
            {
                mp3Url += html.Substring(k, 1);
                k++;
            }

Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);

if (!mp3Url.EndsWith(".mp3"))
            {
                return false;
            }

SetLog("Downloading from DrEye.com");
            SetLog("Mp3 URL: " + mp3Url);
            DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
            return true;
        }

bool DownMp3FromIciba(string word, string localPath)
        {
            //_customerDetail =  _customerDb.GetCustomerById(1);
            string icabaWebUrl = "http://www.iciba.com/";
            string wordWebUrl = icabaWebUrl + word + "/";
            HttpWebRequest webRequest1 = WebRequest.Create(new Uri(wordWebUrl)) as HttpWebRequest;
            HttpWebResponse webResponse1 = webRequest1.GetResponse() as HttpWebResponse;
            Stream stream1 = webResponse1.GetResponseStream();
            StreamReader reader1 = new StreamReader(stream1);
            string html = reader1.ReadToEnd();

string mp3TextStarter = "echoAudio(\"";
            int starterIndex = html.IndexOf(mp3TextStarter);
            int realIndex = starterIndex + mp3TextStarter.Length;
            string mp3Url = "";
            while (html.Substring(realIndex, 1) != "\"")
            {
                mp3Url = mp3Url + html.Substring(realIndex, 1);
                realIndex++;
            }
            Debug.WriteLine("Word:" + word + ", URL; " + mp3Url);
            if (!mp3Url.EndsWith(".mp3"))
            {
                return false;
            }
            SetLog("Downloading from Iciba.com");
            SetLog("Mp3 URL: " + mp3Url);

DownFile(mp3Url, Path.Combine(localPath, word + ".mp3"), true);
            stream1.Close();
            return true;
        }

void DownFile(string fileUrl, string localPath, bool replace)
        {
            if (File.Exists(localPath) && !replace)
            {
                return;
            }

try
            {
                HttpWebRequest webRequest2 = WebRequest.Create(new Uri(fileUrl)) as HttpWebRequest;
                Stream stream2 = webRequest2.GetResponse().GetResponseStream();//TODO:Don't use GetRequestStream() directly.
                StreamReader sr2 = new StreamReader(stream2);
                FileStream localFile = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamWriter writer = new StreamWriter(localFile);
                int bufferSize = 1024;
                byte[] buffer = new byte[bufferSize];
                int readcount = stream2.Read(buffer, 0, bufferSize);
                while (readcount > 0)
                {
                    localFile.Write(buffer, 0, readcount);
                    readcount = stream2.Read(buffer, 0, bufferSize);
                }
                localFile.Close();
                stream2.Close();               
            }
            catch (Exception)
            {                
                throw;
            }
         
        }

界面示例:

软件下载:

WordMp3Downloader.rar

posted on 2009-09-06 04:43 阿牛-专注金融行业开发 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/rockniu/archive/2009/09/06/1561251.html

从谷歌和译典通能上自动下载英语单词mp3的好方法相关推荐

  1. 有没有大佬知道,有哪些可以在网页上自动填写账号密码并登录的方法?

    有没有大佬知道,有哪些可以在网页上自动填写账号密码并登录的方法?比如说我开了十个隐身窗口,用十个小号来登录这个网站,但是手打太累了.所以想求助万能的大佬,有没有什么方法可以实现自动填写账号密码并登录的 ...

  2. 史上最难英语单词——as的七十二变【猴精】

    全部知识点请进入:专升本英语--学习笔记[知识点全轻松学习]!!! https://blog.csdn.net/liu17234050/article/details/104576823 目录: 一: ...

  3. Active X控件在IE上自动下载并注册

     2017年09月19日 10:14:32 阅读数:836 ActiveX控件对于大多数开发人员来说并不陌生,很多情况下我们都会利用ActiveX控件调用一些证书.ActiveX控件注册分为两种: ...

  4. 谷歌tts android手机自带引擎,自动下载android TTS引擎

    Is there a way to install a language automatically? 是的,但这不会自动发生(未经用户同意),如docs所述: Since the installat ...

  5. android 自动语音,在Android上自动下载离线语音识别语言

    这不是你所希望的答案,在写作的时候,我不相信有一个直接的解决方案.我非常希望被证明是错误的. 我要求加强在很久以前 – here以编程方式提供这个信息 增强建议附加参数RecognizerIntent ...

  6. java ftp上传文件_jaVA使用FTP上传下载文件的问题

    为了实现 FTP上传下载,大概试了两个方法 sun.net.ftp.FtpClient org.apache.commons.net 一开始使用sun.net.ftp.FtpClient,结果发现唯一 ...

  7. 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码...

    和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...

  8. Linux| 向linux服务器上传下载文件方式收集(scp)

    scp [优点]简单方便,安全可靠:支持限速参数 [缺点]不支持排除目录 [用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ssh,并且和ssh 使用相同的认证方式, ...

  9. python下载文件传到服务器_windows上python上传下载文件到linux服务器指定路径【转】...

    从windows上传文件到linux,目录下的文件夹自动创建 #!/usr/bin/env python # coding: utf-8 import paramiko import datetime ...

最新文章

  1. 计算机类自主招生推荐信,自主招生推荐信范文-20210709232503.docx-原创力文档
  2. vue router name命名规范_超完整的Vue入门指导
  3. 人工智能在哪些方面可以对IT运营产生重大影响
  4. Google Maps Android API v2官网例子使用说明
  5. java 32个Java面试必考点
  6. Java中数组以及集合
  7. 《系统集成项目管理工程师》必背100个知识点-24变更的常见原因
  8. 博士生的经验就要传下去
  9. boost::gil::extend_boundary用法的测试程序
  10. kie-api_7.0上的新KIE持久性API
  11. 泣血推荐丨学编程开始前就该知道的工具集,提升五倍学习效率
  12. 【CSP201312-1 】出现次数最多的数,排序后扫描并记录
  13. java树遍历算法_Java递归算法实现目录树的遍历
  14. 黑客帝国“01”瀑布流C++
  15. 用计算机弹琴慢速度,弹钢琴的速度如何才能快起来?
  16. chipgenius芯片精灵v4|chipgenius芯片精灵 usb检测工具绿色版v4.00.1024下载
  17. php 同比增长率上期未0,同比增长率计算时,上期值为0怎么计算?
  18. 基于Html的个人展示网站设计与实现
  19. YApi 高级mock脚本 1.8.3版本后,mockJson不能正确返回问题
  20. 求定积分的不太常见的方法

热门文章

  1. 前端中高级基础知识面试汇总
  2. python doc、ppt、excel转pdf
  3. Sentinel-5P数据介绍与处理
  4. [USACO 1.3.2] Barn Repair 修理牛棚
  5. 系统解剖学 | 周围神经系统 | 脑神经
  6. python调用mysql数据进行计算_python使用peewee实现mysql数据操作
  7. 最全的国内大学毕业论文LaTeX模板集合
  8. 360良医2.0 一个规范的智能医疗信息平台,才能推动互联网医疗
  9. 使用Delphi学COM
  10. linux 根目录文件夹解释