asp.net c# 調用百度pai實現線上翻譯,英文轉中文

來源:互聯網
上載者:User

首先去百度:註冊個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;
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.