欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 08:51:12
首先去百度:注册个apikey http://developer.baidu.com/wiki/index.php?title=%e5%b8%ae%e5%8a%a9%e6%...
首先去百度:注册个apikey
http://developer.baidu.com/wiki/index.php?title=%e5%b8%ae%e5%8a%a9%e6%96%87%e6%a1%a3%e9%a6%96%e9%a1%b5/%e7%99%be%e5%ba%a6%e7%bf%bb%e8%af%91api
然后代码如下只看红色部分就可以了:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.script.serialization;
using system.text.regularexpressions;
namespace fangyi
{
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
gsentities db = new gsentities();
for (int i = 740; i < 900000; i++)
{
try
{
system.threading.thread.sleep(100);
geturl("http://www.52mvc.com/story/love/" + i + ".html",db); //采集
}
catch (exception ex)
{
system.threading.thread.sleep(1500);
}
}
db.dispose();
}
private void geturl(string url, gsentities db)
{
content cc = new content();
string apikey = "wqlofg9o2vs1lrix4mz3mdj8";
var enstr = gethtml.gethtml(url, null);
var title = regex.match(enstr.replace("\n",""), @"<font style\=""font\-size\: 18px\; font\-weight\: bold\;""\>(.+?)\<\/font\>").groups[1].value;
enstr = regex.replace(enstr, @"\r|\n", "");
enstr = rehtml(enstr);
enstr = regex.match(enstr, @"双击或拖选\)(.+?) ").groups[1].value;
var atitle = gethtml.gethtml("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="
+ apikey + "&q=" + title + "&from=auto&to=auto", null);
javascriptserializer jss2 = new javascriptserializer();
var aa2 = jss2.deserialize<jsonss>(atitle);
title = aa2.trans_result.first().dst;
enstr = enstr.trim();
string val = "\t\t";
if (enstr.length < 2000)
{
int i = 0;
var list = enstr.replace(" ", "").replace(""", "“").split('.').tolist();
list.foreach(c =>
{
var a = gethtml.gethtml("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="
+ apikey + "&q=" + c + "&from=auto&to=auto", null);
javascriptserializer jss = new javascriptserializer();
var aa = jss.deserialize<jsonss>(a);
val += aa.trans_result.first().dst + "。";
++i;
if (i % 5 == 0)
{
system.threading.thread.sleep(15000);
val += "\n\t\t";
}
system.threading.thread.sleep(1000);
});
cc.title = title;
cc.val = val;
db.content.addobject(cc);
db.savechanges();
}
}
public string rehtml(string html)
{
string pattern = @"<[\s\s]*?>";
regex regex = new regex(pattern);
html = regex.replace(html, "");
return html;
}
//中文转为unicode字符 forget never
public string ctu(string str)
{
string outstr = "";
if (!string.isnullorempty(str))
{
for (int i = 0; i < str.length; i++)
{
//将中文字符转为10进制整数,然后转为16进制unicode字符
outstr += "\\u" + ((int)str[i]).tostring("x");
}
}
return outstr;
}
//unicode字符转为中文
public string utc(string str)
{
string outstr = "";
if (!string.isnullorempty(str))
{
string[] strlist = str.replace("\\", "").split('u');
try
{
for (int i = 1; i < strlist.length; i++)
{
//将unicode字符转为10进制整数,然后转为char中文字符 asp.net
outstr += (char)int.parse(strlist[i], system.globalization.numberstyles.hexnumber);
}
}
catch (formatexception ex)
{
outstr = ex.message;
}
}
return outstr;
}
}
}