从谷歌和译典通能上自动下载英语单词mp3的好方法
生活随笔
收集整理的這篇文章主要介紹了
从谷歌和译典通能上自动下载英语单词mp3的好方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
從谷歌和譯典通能上自動(dòng)下載英語單詞mp3的好方法
??????? {
??????????? 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;
??????????? }
?????????
??????? }
最近在學(xué)英語,很多單詞不怎么會(huì)讀.每次得去iciba.com和dreye.com上查,很是麻煩!
后來一想,能不能寫個(gè)程序自動(dòng)下載?測試一番,最終搞定!
下載的都是真人語音mp3,效果很棒!谷歌愛詞霸是女聲發(fā)音,譯典通是男聲發(fā)音,各有千秋.
?
核心的代碼:
??????? {
??????????? 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?阿牛-專注金融行業(yè)開發(fā) 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/rockniu/archive/2009/09/06/1561251.html
總結(jié)
以上是生活随笔為你收集整理的从谷歌和译典通能上自动下载英语单词mp3的好方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webpack从零开始第1课:安装web
- 下一篇: 聊聊 Kafka: 在 Linux 环境