日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/26 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从谷歌和译典通能上自动下载英语单词mp3的好方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從谷歌和譯典通能上自動下載英語單詞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的好方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。