Asp.net calls Baidu pai online translation

Source: Internet
Author: User

Register Baidu apikey

Http://developer.baidu.com/wiki/index.

Php? Title = % E5 % B8 % AE % E5 % 8A % A9 % E6 % 96% E6 % A1 % A3 % E9 % A6 % 87% E9 % A1 % B5/% E7 % 96% BE % E5 % BA % A6 % E7 % BF % BB % E8 % AF % 91API
Then the code is as follows:

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); // collection
}
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, @ "double-click or drag/drop) (. + ?)
"). 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 = "tt ";
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> ();
Val + = aa. trans_result.First (). dst + ". ";
++ I;
If (I % 5 = 0)
{
System. Threading. Thread. Sleep (15000 );
Val + = "ntt ";
}

 


System. Threading. Thread. Sleep (1000 );
});
Cc. title = title;
Cc. val = val;
Db. content. AddObject (cc );
Db. SaveChanges ();
}
}
Public string ReHtml (string HTML)
{
String pattern = @ "<[sS] *?> ";
Regex regex = new Regex (pattern );
HTML = regex. Replace (HTML ,"");
Return HTML;
}


// Convert Chinese characters to UNICODE characters forget never


Public string ctu (string str)
{
String outStr = "";
If (! String. IsNullOrEmpty (str ))
{
For (int I = 0; I <str. Length; I ++)
{
// Convert the Chinese character to a 10-digit integer and then to a hexadecimal unicode Character
OutStr + = "u" + (int) str [I]). ToString ("x ");
}
}
Return outStr;
}


// Convert UNICODE characters to Chinese Characters


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 ++)
{
// Convert the unicode character to a 10-digit integer and then to the char Chinese Character asp.net
OutStr + = (char) int. Parse (strlist [I], System. Globalization. NumberStyles. HexNumber );
}
}
Catch (FormatException ex)
{
OutStr = ex. Message;
}
}
Return outStr;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.