mycmd系列,抓GOOGLE的翻译做自己的翻译
是一个Console的app
使用方法 :把Translate.exe放到system32的文件夹中,打开cmd,输入Translate空格hello ,就可以查到hello在google上的解释。
如果不放在system32中就要自己设定系统环境变量Path
使用的是.net4.0。
源码:
1 using System.Net;
2 using System.IO;
3 using System.Text;
4 using System;
5
6 namespace Translate
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 HttpWebRequest webrequest = HttpWebRequest.Create(@"http://www.google.com.hk/search?hl=zh-CN&q=fy%20"+args[0]) as HttpWebRequest;
13 webrequest.CookieContainer = new CookieContainer();
14 HttpWebResponse webresponse = webrequest.GetResponse() as HttpWebResponse;
15 using (Stream strM = webresponse.GetResponseStream())
16 {
17 using (StreamReader sr = new StreamReader(strM, Encoding.GetEncoding("gb2312")))
18 {
19 Console.WriteLine(sr.ReadToEnd().Filter());
20 }
21 }
22
23 }
24
25 }
26 public static class HtmlExtern
27 {
28 /// <summary>
29 /// author:Snowleung
30 /// </summary>
31 /// <param name="TempContent"></param>
32 /// <returns></returns>
33 public static string Filter(this string TempContent)
34 {
35 string x=TempContent.regularExpressionsOfHTML();
36 int start = x.IndexOf("您想要的语言");
37 int last = x.IndexOf("translate");
38 return x.Substring(start, last - start);
39 }
40 /// <summary>
41 /// This method from :http://www.cnblogs.com/yuanjw/archive/2007/05/23/756724.html
42 /// </summary>
43 /// <param name="TempContent"></param>
44 /// <returns></returns>
45 public static string regularExpressionsOfHTML(this string TempContent)
46 {
47 //TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]+>",""); //任意多个
48 TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent, "<[^>]*>", ""); //匹配一个
49 return TempContent;
50 }
51 }
2 using System.IO;
3 using System.Text;
4 using System;
5
6 namespace Translate
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 HttpWebRequest webrequest = HttpWebRequest.Create(@"http://www.google.com.hk/search?hl=zh-CN&q=fy%20"+args[0]) as HttpWebRequest;
13 webrequest.CookieContainer = new CookieContainer();
14 HttpWebResponse webresponse = webrequest.GetResponse() as HttpWebResponse;
15 using (Stream strM = webresponse.GetResponseStream())
16 {
17 using (StreamReader sr = new StreamReader(strM, Encoding.GetEncoding("gb2312")))
18 {
19 Console.WriteLine(sr.ReadToEnd().Filter());
20 }
21 }
22
23 }
24
25 }
26 public static class HtmlExtern
27 {
28 /// <summary>
29 /// author:Snowleung
30 /// </summary>
31 /// <param name="TempContent"></param>
32 /// <returns></returns>
33 public static string Filter(this string TempContent)
34 {
35 string x=TempContent.regularExpressionsOfHTML();
36 int start = x.IndexOf("您想要的语言");
37 int last = x.IndexOf("translate");
38 return x.Substring(start, last - start);
39 }
40 /// <summary>
41 /// This method from :http://www.cnblogs.com/yuanjw/archive/2007/05/23/756724.html
42 /// </summary>
43 /// <param name="TempContent"></param>
44 /// <returns></returns>
45 public static string regularExpressionsOfHTML(this string TempContent)
46 {
47 //TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]+>",""); //任意多个
48 TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent, "<[^>]*>", ""); //匹配一个
49 return TempContent;
50 }
51 }
52 }
附上Solution:(vs2010)