百度提供的demo是Python2,在Python3上各种报错,于是小改了下,凑合能用
via http://api.fanyi.baidu.com/api/trans/product/apidoc
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 http.client
import json
import random
from hashlib import md5
from urllib.parse import quote
appid = '20151113000005349'
secretKey = 'osubCEzlGjzvw8qdQc41'
myurl = '/api/trans/vip/translate'
q = 'Long time no see'
fromLang = 'en'
toLang = 'zh'
salt = random.randint(32768, 65536)
sign = appid + q + str(salt) + secretKey
m1 = md5()
m1.update(sign.encode(encoding='utf-8'))
sign = m1.hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
# httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient = http.client.HTTPSConnection('fanyi-api.baidu.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
result = json.loads(response.read().decode(encoding='utf-8'))
print(result)
print(result['trans_result'][0]['dst'])
except Exception as e:
print(e)
finally:
if httpClient:
httpClient.close()
|
打印结果:
1
2
|
{'from': 'en', 'to': 'zh', 'trans_result': [{'src': 'Long time no see', 'dst': '好久不见'}]}
好久不见
|