WEB开发网
开发学院WEB开发ASP.NET asp.net实现调用百度pai 在线翻译英文转中文 阅读

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

 2012-12-18 14:09:40 来源:WEB开发网   
核心提示: 注册个百度apikeyhttp://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然后代码如下

 注册个百度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]*?>";

1 2  下一页

Tags:asp net 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接