zh2000
#-*- 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("\033[1;31;40m请输入翻译内容,然后按Enter,(退出请按A,然后按Enter):\033[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("\033[1;31;40m翻译的结果是:%s\033[0m" % translate_results)

 

分类:

技术点:

相关文章:

  • 2021-06-22
  • 2022-01-28
  • 2021-12-11
  • 2021-12-16
  • 2022-01-06
  • 2021-10-20
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2021-07-16
相关资源
相似解决方案