国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:23:28
字體:
來源:轉載
供稿:網友
首先去百度:注冊個apikey
?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
+ 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 += "http://u" + ((int)str[i]).ToString("x");
}
}
return outStr;
}
//UNICODE字符轉為中文
public string utc(string str)
{
string outStr = "";
if (!string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace("http://", "").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;
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 萝北县| 松阳县| 平安县| 红安县| 周至县| 微山县| 五莲县| 敦煌市| 交口县| 蚌埠市| 永福县| 九龙坡区| 宜良县| 汕尾市| 南召县| 扎鲁特旗| 乌恰县| 庄浪县| 宜章县| 东阿县| 景宁| 唐河县| 通道| 奎屯市| 贺兰县| 鄂托克旗| 信丰县| 扶风县| 富源县| 叙永县| 和平县| 宝坻区| 临城县| 舞阳县| 泸西县| 新建县| 宝兴县| 玉田县| 汝州市| 萝北县| 景东|