Write Baidu translation API with Python3+Requests library by yourself

Ali has launched product pages in a few languages, and wants to automatically translate some English listings into Spanish and other languages ​​and send them back. So prepare to use Baidu Translate API. Automatic translation in 28 languages, free translation of 2 million words per month, do you have all kinds of hearts!

As a result, after downloading the python demo of baidu, I found that his code is like this:

#/usr/bin/env python
#coding=utf8
import httplib
import md5
import urllib
import random

appid = '20151113000005XXX'
secretKey = 'osubCEzlGjzvw8qdQXXX'
httpClient = None
myurl = '/api/trans/vip/translate'
q = 'apple'
fromLang = 'en'
toLang = 'zh'
salt = random.randint (32768, 65536)
sign = appid+q+str(salt)+secretKey
m1 = md5.new()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+urllib.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
try:
    httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
    httpClient.request('GET', myurl)
    #response is the HTTPResponse object
    response = httpClient.getresponse()
    print response.read()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

Is there any mess, I have never seen httplib this stuff! httpClient = httplib.HTTPConnection('api.fanyi.baidu.com') What the hell is this? Do I have to relearn httplib first? Regardless of him, run it again first. As soon as the result is run, it shows that the print format is wrong, and I immediately understand that this Nima is still a demo written by python2. When was something so old written? . . . . . .

Take it easy and think about it, isn't this just a post with a link with content that needs to be translated, this is the era of python3, of course, we need to use the requests library designed for humans! This Baidu, although it is free, is really too perfunctory. . .

Well, do it yourself!

The Baidu translation api documentation requires this:

You need to send the following fields to this address via POST or GET method to access the service

Field Name Type Required Parameters Description Remarks
q TEXT Y Request translation query UTF-8 encoding
from TEXT Y translation source language language list (can be set to auto)
to TEXT Y target language language list (cannot be set to auto)
appid INT Y APP ID can be viewed in the management console
salt INT Y random number	
sign TEXT Y MD5 value of signature appid+q+salt+key

In other words, I want to post a link with 6 parameters of q, from, to, appid, salt, sign.

The post method in requests is:

response = requests.post(url,data)

Here, the URL of the url Baidu translation api is http://api.fanyi.baidu.com/api/trans/vip/translate

The data of 6 parameters is constructed in the form of a dictionary in requests:

    data = {'appid' : appid,
            'q' : q,
            'from' : fromLang,
            'to' : toLang,
            'salt': salt,
            'sign' : sign
            }

The last sign is an md5 digital signature. The requirements in the Baidu Translate API documentation are as follows:

The signature generation method is as follows:

1. Convert the APPID (appid) in the request parameters, translate the query (q, note that it is UTF-8 encoded), the random number (salt), and the key assigned by the platform (can be viewed in the management console)

String 1 is obtained by concatenating in the order of appid+q+salt+key.

2. Do md5 on string 1 to get a 32-bit lowercase sign.

That is: first combine these 4 parameters into a string,

    sign_raw = appid + q + salt + secretKey

Then process sing_raw through the hashlib module in python3,

sign = hashlib.md5(sign_raw.encode('utf8')).hexdigest()
Note: The string submitted to hashlib.md5 must be of byte type, so sign_raw.encode('utf8') is required. The following .hexdigest() is required to return a hexadecimal number.

Well, it's that simple. Two steps, one, generate the MD5 signature to form a parameter dictionary, and two, requests.post(url, data), you can get a series of replies from Baidu Translate:

{'from': 'en', 'to': 'zh', 'trans_result': [{'src': 'This is the content that needs to be translated', 'dst': 'This is the content to be translated'}]}

It's that simple! Done in two steps!

full code

# coding=utf-8
import requests
import hashlib
import random

def baidu_fanyi(content,appid,secretKey,fromLang,toLang):
    #generate random numbers
    salt = str(random.randint(32768, 65536))
    #Generate MD5 encrypted signature
    sign_raw = appid + content + salt + secretKey
    sign = hashlib.md5(sign_raw.encode('utf8')).hexdigest()
    #Construct the 6 parameters that need to be passed in by post in a dictionary way
    data = {'appid' : appid,
            'q' : content,
            'from' : fromLang,
            'to' : toLang,
            'salt': salt,
            'sign' : sign
            }
    # Baidu translation api call URL
    url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    
    response = requests.post(url,data)
    print ('Baidu api return value: %s\n'%response.text)
    result = eval(response.text)
    trans_result = result['trans_result'][0]['dst']
    return trans_result
if __name__ == '__main__':
    content = 'I love who I love, hate who I hate'    #待翻译内容
    appid = '20180313000135XXX' #Apply for api to get
    secretKey = 's0oBI7VMkrsm4yDpTXXX' #Apply for api to get
    fromLang = 'en' #Original language
    toLang = 'zh' #target language
    
    trans_result = baidu_fanyi(content,appid,secretKey,fromLang,toLang)
    print (trans_result)




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325347702&siteId=291194637