翻译器

#-*- coding=UTF-8 -*-
#usr/bin/pthon3
#author=zh
#导入模块 request、parse、json from urllib import request from urllib import parse import json #只有直接执行这个脚本时,才会执行以下代码。若通过其他脚本程序载入则不执行以下代码。 while True: content = input("33[1;31;40m请输入翻译内容,然后按Enter,(退出请按A,然后按Enter):33[0m") if content == 'A': break else: #定义有道翻译API接口的URL。 Request_URL = 'http://fanyi.youdao.com/translate' #创建字典Form_Data Form_Data = {} #存储有道翻译Form_Data信息 Form_Data['i'] = content Form_Data['from'] = 'AUTO' Form_Data['to'] = 'AUTO' Form_Data['smartresult'] = 'dict' Form_Data['client'] = 'fanyideskweb' Form_Data['salt'] = '15608449167166' Form_Data['sign'] = 'cc1a182e4d64b0bef4b1ef8998599378' Form_Data['ts'] = '1560844916716' Form_Data['bv'] = 'e2a78ed30c66e16a857c5b6486a1d326' Form_Data['doctype'] = 'json' Form_Data['version'] = '2.1' Form_Data['keyfrom'] = 'fanyi.web' Form_Data['action'] = 'FY_BY_CLICKBUTTION' #使用urlencode方法转换为utf-8格式 data = parse.urlencode(Form_Data).encode('utf-8') #使用urlopen传递Request_URL, data(转换完的数据)到response response = request.urlopen(Request_URL, data) #读取信息,编码为utf-8格式 html = response.read().decode('utf-8') #使用json方法找出所有json信息 translate_results = json.loads(html) #找到翻译结果 translate_results = translate_results['translateResult'][0][0]['tgt'] #输出翻译结果 print("33[1;31;40m翻译的结果是:%s33[0m" % translate_results)
原文地址:https://www.cnblogs.com/zh2000/p/11059164.html