Baidu API ImplementationSTEP1: Application API Key
>>以前用过BAE,已经有了Api Key,没有的可以去申请
STEP2: Very simple, just look at the implementation of the Code bar
>> Python#coding:utf-8import urllib2import urllibimport json# using get method Request URL = ' http://openapi.baidu.com/public/2.0/bmt/ translate?from=%s&to=%s&q=%s&client_id=%s ' Api_key = ' * * * ' #换成自己的APIKeydef trans (from,to, str): url = url% (From, To, Urllib.quote (str), api_key) try:rsp = Urllib2.urlopen (URL) except Exception, e:raise Exception (U ' linked server failed: ' + str (e)) rs = json.load (RSP) if ' Error_code ' in Rs:raise Exception (U ' translation error: ' + rs[' error_msg ']) if rs[' Trans_result ']: Return rs[' Trans_result '][0][' DST ']else:raise Exception (' u no Result ') def zh2en (SRC): return trans (' zh ', ' en ', SRC) def en2 ZH (SRC): return trans (' En ', ' zh ', src) if __name__ = = ' __main__ ': str = raw_input (u) English: 1, English-Chinese: 2 \n>> ;") While true:if str = = 1:STR = raw_input (u "Please enter the Chinese sentence to translate:\n>>") print U ' translation result: ', Zh2en (str) Else:str = raw_input (u "Please enter the English sentence to translate:\n>>") print U ' translation result: ', En2zh (str) ...
Youdao API implementation (not yet)
Python English/Chinese translation (Baidu api/Youdao API)