White school Python-- achieve translation translation API with Baidu

My English is not good, do not know a lot of phrases, only with tools; Baidu and Google translation translation are good, the recent self-Python, wanted if he could design a Baidu translation software?

Baidu open platform translation:  http://api.fanyi.baidu.com/api/trans/product/index

 

 

Baidu open platform Baidu translation translation for the majority of developers to provide open service platform.

Services include: universal translator API, customized translation API, SDK voice translation, translation SDK photographs, etc. , and continue to update. You can use your own common.

Universal API uses translation mode the whole process of self-service applications. Click over the site of the "Login" button to Baidu account login platform; in goods and services page click "Use now" and follow the prompts to register as a page developer, you can get APPID and key information. Baidu account or the same phone number and can only apply for a group of APPID key information , and the APPID key information can be used for a number of service calls

Users registered developer, you can click on the Services page "immediately" or common open API translation service in the management console; To open more services available in the management console - the opening of other services overview page.

 

 

Universal translator API technical documentation    http://api.fanyi.baidu.com/api/trans/product/apidoc

External universal translator API provides multilingual translation services through HTTP interface. You only need to call the universal translator API, incoming content to be translated, and specify the source language for translation (source language supports automatic language detection) and the target language, you can get the appropriate translation.

Universal translator API HTTP address: http: //api.fanyi.baidu.com/api/trans/vip/translate

Universal translator API HTTPS address: https: //fanyi-api.baidu.com/api/trans/vip/translate

 

 

 Complete code:

import json
import random
import hashlib
from urllib import parse
import http.client

class BaiduTranslate:
    def __init__(self,fromLang,toLang):
        self.url = "/api/trans/vip/translate"
        self.appid="xxxxx" #申请的账号
        self.secretKey = 'xxxxx'#账号密码
        self.fromLang = fromLang
        self.toLang = toLang
        self.salt = random.randint(32768, 65536)

    def BdTrans(self,text):
        sign = self.appid + text + str(self.salt) + self.secretKey
        md = hashlib.md5()
        md.update(sign.encode(encoding='utf-8'))
        sign = md.hexdigest()
        myurl = self.url + \
                '?appid=' + self.appid + \
                '&q=' + parse.quote(text) + \
                '&from=' + self.fromLang + \
                '&to=' + self.toLang + \
                '&salt=' + str(self.salt) + \
                '&sign=' + sign
        try:
            httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
            httpClient.request('GET', myurl)
            response = httpClient.getresponse()
            html = response.read().decode('utf-8')
            html = json.loads(html)
            dst = html["trans_result"][0]["dst"]
            return  True , dst
        except Exception as e:
            return False , e
if __name__=='__main__':
    BaiduTranslate_test = BaiduTranslate('en','zh')
    Results = BaiduTranslate_test.BdTrans("Hello, World!") # Phrase to be translated
     Print (Results)

 

Guess you like

Origin www.cnblogs.com/adam012019/p/11441461.html