-
Notifications
You must be signed in to change notification settings - Fork 0
/
baidu.py
40 lines (37 loc) · 1.2 KB
/
baidu.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
37
38
39
40
# /usr/bin/env python
# coding=utf8
import httplib
import md5
import urllib
import random
import json
class translation2:
@classmethod
def translate(self, q):
appid = 'xx' # appid
secretKey = 'xx' # 密钥
httpClient = None
myurl = '/api/trans/vip/translate'
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是HTTPResponse对象
response = httpClient.getresponse()
# print response.read()
data = json.loads(response.read())
data = data["trans_result"][0]["dst"]
return data
except Exception, e:
return ' '
finally:
if httpClient:
httpClient.close()