python百度翻译接口_python调用百度翻译接口

一、申请APIKey

二、关于生成链接

下面内容均整理自百度翻译开放平台接入文档。

5b6350627031?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

返回结果是json格式,包含以下字段:

5b6350627031?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

三、代码实例

1、先写一个能生产md5值的函数

import hashlib

def md5(str):#生成md5

m = hashlib.md5()

m.update(str)

return m.hexdigest()

2、英译中

def en_to_zh(src):#英译中

ApiKey = "xxxxxxxxxxxxxxxxx"

pwd = "***************"

salt = "1435660288"

all = ApiKey + src + salt + pwd

sign = md5(all)

src=src.replace(' ','+')#生成sign前不能替换

url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="\

+ src + "&from=en&to=zh&appid=" + ApiKey + \

"&salt=" + salt + "&sign=" + sign

try:

req = urllib2.Request(url)

con = urllib2.urlopen(req)

res = json.load(con)

if 'error_code' in res:

print 'error:', res['error_code']

return res['error_msg']

else:

dst = res['trans_result'][0]['dst']

return dst

except:

return "出错了"

3、中译英

def zh_to_en(src):#中译英

ApiKey = "xxxxxxxxxxxxxxxxx"

pwd = "***************"

salt = "1435660288"

all = ApiKey + src + salt + pwd

sign = md5(all)

src=src.replace(' ','+')#生成sign前不能替换

url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="\

+ src + "&from=zh&to=en&appid=" + ApiKey + \

"&salt=" + salt + "&sign=" + sign

try:

req = urllib2.Request(url)

con = urllib2.urlopen(req)

res = json.load(con)

return res['trans_result'][0]['dst']

except:

return "出错了"

完整代码见下:

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

import json

import urllib2

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

import hashlib

def md5(str):#生成md5

m = hashlib.md5()

m.update(str)

return m.hexdigest()

def en_to_zh(src):#英译中

ApiKey = "xxxxxxxxxxxxxxxxx"

pwd = "***************"

salt = "1435660288"

all = ApiKey + src + salt + pwd

sign = md5(all)

src=src.replace(' ','+')#生成sign前不能替换

url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="\

+ src + "&from=en&to=zh&appid=" + ApiKey + \

"&salt=" + salt + "&sign=" + sign

try:

req = urllib2.Request(url)

con = urllib2.urlopen(req)

res = json.load(con)

if 'error_code' in res:

print 'error:', res['error_code']

return res['error_msg']

else:

dst = res['trans_result'][0]['dst']

return dst

except:

return "出错了"

def zh_to_en(src):#中译英

ApiKey = "xxxxxxxxxxxxxxxxx"

pwd = "***************"

salt = "1435660288"

all = ApiKey + src + salt + pwd

sign = md5(all)

src=src.replace(' ','+')#生成sign前不能替换

url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="\

+ src + "&from=zh&to=en&appid=" + ApiKey + \

"&salt=" + salt + "&sign=" + sign

try:

req = urllib2.Request(url)

con = urllib2.urlopen(req)

res = json.load(con)

return res['trans_result'][0]['dst']

except:

return "出错了"

def main():

choice = raw_input("English to Chinese:Enter 1 \n"

"Chinese to English:Enter 2 \n"

"Enter:")

if choice == "1":

while True:

word = raw_input("Input the word you want to search:")

print "translate......"

target = en_to_zh(word)

print target

else:

while True:

word = raw_input("Input the word you want to search:")

print "translate......"

target = zh_to_en(word)

print target

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值