python实现在线翻译

时间:2021-09-07 13:27:45

本文实例为大家分享了python实现在线翻译的具体代码,供大家参考,具体内容如下

具体效果请看图

python实现在线翻译

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import urllib.request
import urllib.parse
import json
 
def translation():
  while 1:
    print("-"*30)
    n = input("请选择:1 翻译 2 退出 :")
    if n =='1':
      content = input("请输入要翻译的内容:")
      url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
      data={}
      data['i']= content
      data['from']= 'AUTO'
      data['to']='AUTO'
      data['smartresult'] ='dict'
      data['client']= 'fanyideskweb'
      data['salt']= '15790094838498'
      data['sign']= '9ab763875001c1949ae49d3c230ba19f'
      data['ts']= '1579009483849'
      data['bv']= '5a84f6fbcebd913f0a4e81b6ee54608'
      data['doctype']= 'json'
      data['version']= '2.1'
      data['keyfrom']= 'fanyi.web'
      data['action']= 'FY_BY_CLICKBUTTION'
      data = urllib.parse.urlencode(data).encode('utf-8')
      response = urllib.request.urlopen(url,data)
      html = response.read().decode('utf-8')
      #print(json.loads(html))
      target =json.loads(html)
      print("翻译结果;%s" % (target['translateResult'][0][0]['tgt']))
    elif n=='2':
      print("感谢使用!")
      break
    else:
      print("输入有误!")
 
if __name__=='__main__':
  translation()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_42472371/article/details/103981916