import pandas as pd
import requests
import json
import hashlib
import random
import numpy as np
import time
apiurl = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
appid = '20200818000545689'
secretKey = 'l9lrhrQdfwJAv0faWJ7A'
def translateBaidu(content, fromLang='en', toLang='zh'):
salt = str(random.randint(32768, 65536))
sign = appid + content + salt + secretKey
sign = hashlib.md5(sign.encode("utf-8")).hexdigest()
try:
paramas = {
'appid': appid,
'q': content,
'from': fromLang,
'to': toLang,
'salt': salt,
'sign': sign
}
response = requests.get(apiurl, paramas)
jsonResponse = response.json()
dst = str(jsonResponse["trans_result"][0]["dst"])
return dst
except Exception as e:
print(e)
with open(r'C:\Users\zxzy\Desktop\trans.txt','r',encoding = 'utf-8') as f:
content = f.readlines()
with open(r'C:\Users\zxzy\Desktop\translate.txt','a+',encoding = 'utf-8') as f:
for i in content:
tr = translateBaidu(str(i))
f.writelines(str(tr)+"\n")