首先去百度:注册个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("&nbsp;", "").Replace("&quot;", "“").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;
}
}
}
标签:
百度pai,在线翻译,英转中

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。