Baidu translation experience (Python version)

1. Introduction to Baidu Translation

Optional mode

  • Universal Translation API-Standard Edition: completely free, QPS=1
  • Universal Translation API-Advanced Edition (personal authentication is required), 2 million characters free/month; more than 2 million characters, pay for the excess characters of the month at RMB 49 /million characters (QPS is 10).
  • Universal Translation API-Premium Edition: Enterprise certification is required, and the billing method is the same as above.

Inputs and outputs (overview)

Output form: trans_result
trans_result is an array, each of which { }is a paragraph, and its structure is as follows:

trans_result: [{
    
    },{
    
    },{
    
    }]

The paragraph result is one item in the trans_result array:

{
    
    “src”:””,“dst”:””}

Some parameter description

  • q: request translation query
  • from: translation source language
  • to: target language
  • appid:APP ID
  • salt: random number
  • sigh: The MD5 value
    signature of appid+q+salt+key is to ensure the security of the call. A string generated by the MD5 algorithm, the length of the generated signature is 32 bits, and the English characters in the signature are all lowercase.

2. Example of use

This test translates English words appleinto Chinese.

# 百度通用翻译API,不包含词典、tts语音合成等资源,如有相关需求请联系[email protected]
# coding=utf-8

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

appid = 'xxxxx'  # 填写你的appid
secretKey = 'xxxxx'  # 填写你的密钥

httpClient = None
myurl = '/api/trans/vip/translate'

fromLang = 'auto'   #原文语种
toLang = 'zh'   #译文语种
salt = random.randint(32768, 65536)
q = 'apple' # 待翻译字符串
sign = appid + q + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign

print(f'myurl: {myurl}')

try:
    httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
    httpClient.request('GET', myurl)

    # response是HTTPResponse对象
    response = httpClient.getresponse()
    result_all = response.read().decode("utf-8")
    result = json.loads(result_all)

    print (f'result: {result}')

except Exception as e:
    print (e)
finally:
    if httpClient:
        httpClient.close()

The print result is as follows:

myurl:  /api/trans/vip/translate?appid=xxxxx&q=apple&from=auto&to=zh&salt=40068&sign=70ee84dc8e4d1bd75dc7db159d63c1a9
result:  {
    
    'from': 'en', 'to': 'zh', 'trans_result': [{
    
    'src': 'apple', 'dst': '苹果'}]}

3. Possible problems

Error code list

Insert picture description here

Various limitations of Baidu Translation API

If there is an error in the program, you can consider the following possible aspects.

1. Account type
Standard version: basic translation is free to use, unlimited characters, QPS (visits per second) is 1.

Premium version: 2 million characters per month for free, QPS is 10

2. Interface restrictions The
default limit of Baidu Translation API usage frequency is 1000 times per hour per IP.

3. IP restriction If the
same IP uses multiple APPIDs to send translation requests on the same day, the IP will be blocked to request permission on the same day, and the ban will be unblocked the next day. (58003 error)

4. The upper limit of a single translation: 6000 bytes.
Query length: To ensure the quality of translation, please control the length of a single request within 6000 bytes. (About 2000 Chinese characters)

reference:

General translation API technical documentation

Guess you like

Origin blog.csdn.net/Robin_Pi/article/details/114261008