python调用百度翻译接口
来源:互联网 发布:ptc公司的软件 编辑:程序博客网 时间:2024/06/05 06:17
一、申请APIKey
到百度翻译开放平台申请APIKey,链接:http://api.fanyi.baidu.com/api/
二、关于生成链接
下面内容均整理自百度翻译开放平台接入文档。
返回结果是json格式,包含以下字段:
三、代码实例
1、先写一个能生产md5值的函数
import hashlibdef md5(str):#生成md5 m = hashlib.md5() m.update(str) return m.hexdigest()
2、英译中
def trans(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) 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 "出错了"
完整代码见下:
#-*-coding:utf-8-*-import jsonimport urllib2import sysreload(sys)sys.setdefaultencoding('utf-8')import hashlibdef md5(str):#生成md5 m = hashlib.md5() m.update(str) return m.hexdigest()def trans(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) 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 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 targetif __name__ == '__main__': main()
阅读全文
0 0
- python调用百度翻译接口
- Python 调用 百度翻译
- python调用百度翻译
- C#调用百度翻译接口小程序
- 调用百度翻译机器人接口纯代码
- python3调用百度翻译接口样例
- python自动化接口测试(百度翻译)
- QtQuick2实现英汉翻译--后台调用百度翻译接口
- 调用百度翻译接口demo(完整ajax请求)
- 百度翻译接口
- 百度翻译接口
- 调用百度翻译API
- C#调用百度翻译
- python3调用百度翻译
- 实践项目五:python调用百度API实现自动检测翻译
- 【Python】Python的urllib、urllib2模块调用“百度翻译”API进行批量自动翻译
- python爬虫百度翻译
- 百度翻译C#API接口
- Io流初学
- MySQL Reset Password On CentOS 6.7
- javaScript捕获,冒泡
- 最详细的Vlan原理介绍
- poj 1276 多重背包模板
- python调用百度翻译接口
- Hibernate no session 错误解决方法
- StringBuffer和StringBuilder实现浅析
- 35个Java代码性能优化总结
- 个人对IO流中字节流的粗浅认知
- spring_spring自动装配
- zlib库使用简单讲解
- 菜鸟学php扩展 之 详解php扩展的变量(四)
- [leetcode]: 108. Convert Sorted Array to Binary Search Tree