熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net實現調用百度pai 在線翻譯英文轉中文

2013-11-13 10:37:52  來源: .NET編程 

  注冊個百度apikey

  php?title=%E%B%AE%E%A%A%E%%%E%A%A%E%A%%E%A%B/%E%%BE%E%BA%A%E%BF%BB%E%AF%API
然後代碼如下只看紅色部分就可以了:

  using System;

  using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebScriptSerialization;
using SystemTextRegularExpressions;

  
namespace Fangyi
{
public partial class _Default : SystemWebUIPage
{

protected void Page_Load(object sender EventArgs e)
{
GSEntities db = new GSEntities();
for (int i = ; i < ; i++)
{

  
try
{
SystemThreadingThreadSleep();
GetUrl(""db); //采集
}
catch (Exception ex)
{
SystemThreadingThreadSleep();
}
}
dbDispose();
}

  
private void GetUrl(string url GSEntities db)
{
content cc = new content();
string apikey = "WqLOfGoVSlriXmzmDj";
var enstr = GetHtmlgetHtml(url null);
var title = RegexMatch(enstrReplace("n""") @"<font style=""fontsize: px; fontweight: bold;"">(+?)</font>")Groups[]Value;
enstr = RegexReplace(enstr @"r|n" "");
enstr = ReHtml(enstr);
enstr = RegexMatch(enstr @"雙擊或拖選)(+?)
")Groups[]Value;
var atitle = GetHtmlgetHtml("
+ apikey + "&q=" + title + "&from=auto&to=auto" null);
JavaScriptSerializer jss = new JavaScriptSerializer();
var aa = jssDeserialize<jsonss>(atitle);
title = aatrans_resultFirst()dst;
enstr = enstrTrim();
string val = "tt";
if (enstrLength < )
{

int i = ;
var list = enstrReplace(" " "")Replace(""" "“")Split()ToList();
listForEach(c =>
{
var a = GetHtmlgetHtml("
+ apikey + "&q=" + c + "&from=auto&to=auto" null);
JavaScriptSerializer jss = new JavaScriptSerializer();
var aa = jssDeserialize<jsonss>(a);
val += aatrans_resultFirst()dst + "";
++i;
if (i % == )
{
SystemThreadingThreadSleep();
val += "ntt";
}

  
SystemThreadingThreadSleep();
});
cctitle = title;
ccval = val;
dbcontentAddObject(cc);
dbSaveChanges();
}
}
public string ReHtml(string HTML)
{
string pattern = @"<[sS]*?>";
Regex regex = new Regex(pattern);
HTML = regexReplace(HTML "");
return HTML;
}

  
//中文轉為UNICODE字符 forget never

  
public string ctu(string str)
{
string outStr = "";
if (!stringIsNullOrEmpty(str))
{
for (int i = ; i < strLength; i++)
{
//將中文字符轉為進制整數然後轉為進制unicode字符
outStr += "u" + ((int)str[i])ToString("x");
}
}
return outStr;
}

  
//UNICODE字符轉為中文

  
public string utc(string str)
{
string outStr = "";
if (!stringIsNullOrEmpty(str))
{
string[] strlist = strReplace("" "")Split(u);
try
{
for (int i = ; i < strlistLength; i++)
{
//將unicode字符轉為進制整數然後轉為char中文字符 aspnet
outStr += (char)intParse(strlist[i] SystemGlobalizationNumberStylesHexNumber);
}
}
catch (FormatException ex)
{
outStr = exMessage;
}
}
return outStr;
}
}
}


From:http://tw.wingwit.com/Article/program/net/201311/14160.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.