Python 20行简单实现有道在线翻译的详解
作者:kongfu_cat 时间:2022-02-12 22:19:29
简介
主要是尝试简单的使用pyhton的爬虫功能,于是使用有道进行尝试,并没有进行深入的诸如相关api的调用。
以下是需要的POST数据
代码
以下是相关部分的代码:
import urllib.request
import urllib.parse
import json
content=input('需要翻译的内容:')
#翻译内容
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=http://fanyi.youdao.com/'
#有道翻译查询入口
data = { #表单数据
'i': content,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
'typoResult': 'false'
}
data=urllib.parse.urlencode(data).encode('utf-8')
#对POST数据进行编码
response=urllib.request.urlopen(url,data)
#发出POST请求并获取HTTP响应
html=response.read().decode('utf-8')
#获取网页内容,并进行解码解码
target=json.loads(html)
#json解析
print("\n翻译结果:%s"%target['translateResult'][0][0]['tgt'])
#输出翻译结果
重要函数
urllib.request.urlopen()——发送POST数据,同时返回响应
urllib.parse.urlencode()——对POST数据进行编码转换
json.loads()——进行json解析
以上所述是小编给大家介绍的Python实现有道在线翻译的方法详解整合网站的支持!
来源:https://blog.csdn.net/kongfu_cat/article/details/79682030
标签:Python,有道翻译
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
flask重启后端口被占用的问题解决(非kill)
2021-06-24 16:11:24
Python日志logging模块功能与用法详解
2022-07-18 16:54:04
![](https://img.aspxhome.com/file/2023/1/107991_0s.png)
python 实现简单的FTP程序
2021-03-29 10:33:00
![](https://img.aspxhome.com/file/2023/2/85382_0s.jpg)
python实现list由于numpy array的转换
2023-08-05 11:08:19
python实现折半查找和归并排序算法
2023-06-24 00:08:01
![](https://img.aspxhome.com/file/2023/6/101226_0s.jpg)
python自动翻译实现方法
2022-01-01 08:03:31
Django发送邮件和itsdangerous模块的配合使用解析
2023-09-11 04:29:00
python pyqtgraph 保存图片到本地的实例
2023-05-08 01:15:11
![](https://img.aspxhome.com/file/2023/4/77764_0s.jpg)
Python jieba库用法及实例解析
2021-04-15 22:11:44
![](https://img.aspxhome.com/file/2023/8/97598_0s.png)
Python爬取网页中的图片(搜狗图片)详解
2023-06-24 00:34:45
![](https://img.aspxhome.com/file/2023/2/70852_0s.jpg)
关于设计的一些个人看法
2008-06-12 12:44:00
Python基础之循环语句相关知识总结
2021-03-19 18:57:19
![](https://img.aspxhome.com/file/2023/5/105395_0s.png)
python IP地址转整数
2023-06-08 22:38:25
Python中实现最小二乘法思路及实现代码
2021-06-17 02:23:24
![](https://img.aspxhome.com/file/2023/9/90409_0s.png)
Python中多线程thread与threading的实现方法
2021-08-24 08:34:38
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2022-04-07 09:50:44
![](https://img.aspxhome.com/file/2023/1/101721_0s.gif)
python argparse模块传参用法实例
2022-01-19 09:53:42
解决MySQL 5数据库连接超时问题
2009-03-25 15:24:00
pytorch程序异常后删除占用的显存操作
2021-04-03 22:33:38
详解如何使用Python编写vim插件
2023-01-08 13:30:39