博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
有道词典的翻译接口
阅读量:4311 次
发布时间:2019-06-06

本文共 1081 字,大约阅读时间需要 3 分钟。

import json import requests # 翻译函数,word 需要翻译的内容 def translate(word):     # 有道词典 api     url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'     # 传输的参数,其中 i 为需要翻译的内容     key = {
'type': "AUTO", 'i': word, "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "ue": "UTF-8", "action": "FY_BY_CLICKBUTTON", "typoResult": "true" } # key 这个字典为发送给有道词典服务器的内容 response = requests.post(url, data=key) # 判断服务器是否相应成功 if response.status_code == 200: # 然后相应的结果 print('111') return response.text else: print("有道词典调用失败") # 相应失败就返回空 return None def get_reuslt(repsonse): # 通过 json.loads 把返回的结果加载成 json 格式 result = json.loads(repsonse) return result['translateResult'][0][0]['tgt'] def main(err): word = err list_trans = translate(word) return get_reuslt(list_trans) print(main('SyntaxError: bad input on line 1'))

转载于:https://www.cnblogs.com/duanpengpeng/p/11149365.html

你可能感兴趣的文章
python字符串常用函数
查看>>
数据结构
查看>>
列表生成式,生成器表达式,模块
查看>>
Android注解框架实战-ButterKnife(原创)
查看>>
三、回归问题与应用
查看>>
第二届PHP全球开发者大会(含大会的PPT)
查看>>
5.23BOM
查看>>
SVN使用教程
查看>>
献给初学者:谈谈如何学习Linux操作系统
查看>>
vb中的反正弦函数
查看>>
Match:Keywords Search(AC自动机模板)(HDU 2222)
查看>>
ASM:《X86汇编语言-从实模式到保护模式》第16章:Intel处理器的分页机制和动态页面分配...
查看>>
CORS’s source, principle and implementation
查看>>
分割字符串
查看>>
选择排序
查看>>
线性表 - 公式化描述实现线性表
查看>>
javaweb搭建云服务器环境
查看>>
referer——防盗链
查看>>
有callback的回调中,不能直接更新UI的解决办法
查看>>
HDU 4123(树上任意点到其他点的最远距离,rmq
查看>>