Python 20-line simple implementation of Youdao online translation

Please indicate the source

content

Introduction

The main purpose is to simply use the crawler function of pyhton, so I use Youdao to try, and I did not make in-depth calls such as related apis.
Below is the required POST data
write picture description here

code

Here is the relevant part of the code:

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'])
#输出翻译结果

important function

  • urllib.request.urlopen() - sends POST data and returns a response

  • urllib.parse.urlencode() - encode and convert POST data

  • json.loads() - json parsing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325369806&siteId=291194637