-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.py
36 lines (30 loc) · 1.03 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf8
import http.client
import hashlib
import urllib.parse
import random
import json
def translate(q="This is a good day", fromLang="en", toLang="zh"):
appid = ''
secretKey = ''
httpClient = None
myurl = '/api/trans/vip/translate'
salt = random.randint(32768, 65536)
sign = appid+q+str(salt)+secretKey
m1 = hashlib.md5()
m1.update(sign.encode())
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
result = ""
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
#response是HTTPResponse对象
response = httpClient.getresponse()
result = response.read()
temp = json.loads(result)
result = temp['trans_result'][0]['dst']
return result
if __name__ == '__main__':
res = translate('This is a good day', fromLang="en", toLang="zh")
# res = translate(u'今天是个好日子', fromLang="zh", toLang="en")
print(res)