Python calls Baidu Translation API

Source: Internet
Author: User

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

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.