import requests
import random
import json
from hashlib import md5
appid = '2xxx'
appkey = 'kxxx'
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
query = 'Hello World! This is 1st paragraph.This is 2nd paragraph.'
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
def baidu_api(query,from_lang,to_lang):
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
r = requests.post(url, params=payload, headers=headers)
result = r.json()
return result["trans_result"][0]['dst']
print(baidu_api(query,from_lang, to_lang))