asp.net c# 调用百度pai实现在线翻译,英文转中文
2012-12-19 00:00
926 查看
首先去百度:注册个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; } } }
相关文章推荐
- asp.net c# 调用百度pai 实现在线翻译,英文转中文
- 黑马程序员之asp.net学习笔记:巧用C#做中间语言 实现java调用.net
- ASP.NET C# 货币转换函数 中文大写金额 英文金额
- 【转】ASP.NET实现在线人数的统计(C#)
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
- Google API的全文翻译 asp.net,简单调用实现,源代码
- 在asp.net(c#)下实现调用cmd的方法
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- Asp.net C#下实现运行cmd调用命令的代码实例
- asp.net(c#)用Ajax调用web 服务实现省市县三级联动
- ASP.NET中在线用户统计 application和cache实现缓存的差异 ASP.net 验证码(C#)
- C# Asp.Net自动调用百度搜索页面baidu/google搜索页面
- ASP.NET]ASP.NET C# 用 Print2Flash 实现在线预览 Word、PPT、PDF 等
- Asp.NET调用百度翻译的方法
- Asp.net C# 实现内容页调用模板页函数
- Asp.NET调用百度翻译的方法
- ASP.NET C# 货币转换函数 中文大写金额 英文金额
- ASP.NET C# 实现实时用户在线