asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net实现调用百度pai 在线翻译英文转中文


发布日期:2018年10月08日
 
asp.net实现调用百度pai 在线翻译英文转中文

注册个百度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;
}
}
}

               

上一篇:ASP.NET常用6种验证控件介绍

下一篇:ASP.NET中获取URL重写前的原始地址