Python 20行简单实现有道在线翻译

转载请注明出处

目录

简介

主要是尝试简单的使用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解析

猜你喜欢

转载自blog.csdn.net/kongfu_cat/article/details/79682030
今日推荐