As the internship company to do is the Japanese app, sometimes to look at user feedback, for me this 50-tone picture has not been remembered, said Baidu translation can indeed. But do not want to copy and paste every time, Google is the wall is also a very painful thing, so use Python combined with the Baidu API for a moment, and you share.
1. Baidu Translation API
Because Baidu translation API needs to use the API key, so, you have to register Baidu developer account, and then create a developer service project, get the authorization API key, specific operation can view official documents, please click Baidu translation API
2. Code implementation
The basic idea is: first to grasp the user feedback, and then process the HTML tags, and then call Baidu Translation API, the user feedback content (Japanese) translated into Chinese, here paste a section of Baidu translation code.
#-*-coding:utf-8-*-ImportstringImportRe,osImportJSONImportUrllib2Importsys Reload (SYS) sys.setdefaultencoding ('Utf-8') #call Baidu Translation APIdefTrans_baidu (SRC): ApiKey="xxxxxxxxxxxxxxxxxxx"#Baidu developer ApikeyTurl ="http://openapi.baidu.com/public/2.0/bmt/translate?client_id="+apikey+"&q="+src+"&from=auto&to=zh" Try: Req=Urllib2. Request (Turl) con=Urllib2.urlopen (req). Read ()exceptException, E:RaiseeElse: Decoded=json.loads (con) DST= STR (decoded["Trans_result"][0]["DST"]) returnDSTdefMain (): whileTrue:word= Raw_input ('Input The word want to search:') Print "translate ....."Target=Trans_baidu (Word)PrintTargetif __name__=='__main__': Main ()
Python calls Baidu Translation API