亲宝软件园·资讯

展开

python在线翻译 python实现在线翻译

程序员不掉发 人气:0

具体效果请看图

代码:

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()

加载全部内容

相关教程
猜你喜欢
用户评论