百度翻译API pyhon demo

Posted notfresh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度翻译API pyhon demo相关的知识,希望对你有一定的参考价值。

目前从百度翻译下载来的是 python2的

我把pyhon3 的贴出来.

# -*- coding:utf-8 -*-

from http.client import HTTPConnection
import hashlib
import urllib
import random

appid = ‘20151113000005349‘
secretKey = ‘osubCEzlGjzvw8qdQc41‘


httpClient = None
myurl = ‘/api/trans/vip/translate‘
q = ‘apple‘
fromLang = ‘en‘
toLang = ‘zh‘
salt = random.randint(32768, 65536)

sign = appid+q+str(salt)+secretKey
# m1 = md5.new()
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

try:
httpClient = HTTPConnection(‘api.fanyi.baidu.com‘)
httpClient.request(‘GET‘, myurl)

#response是HTTPResponse对象
response = httpClient.getresponse()
print (response.read())
except Exception as e:
print (e)

finally:
if httpClient:
httpClient.close()





































以上是关于百度翻译API pyhon demo的主要内容,如果未能解决你的问题,请参考以下文章

使用百度翻译api支持中转英,中简转中繁等等

用python实现百度翻译

python 百度翻译API的示例代码

带头大哥777:Java调用百度API实现翻译

python百度翻译爬虫

使用百度翻译的API接口