Python realizes online translation

The examples in this article share the specific code for online translation in python for your reference. The specific content is as follows

Please see the picture for the specific effect

Code:

import urllib.request
import urllib.parse
import json

def translation():while1:print("-"*30)
 n =input("Please choose: 1 Translate 2 Exit:")if n =='1':
  content =input("Please enter the content to be translated:")
  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("Translation results;%s"%(target['translateResult'][0][0]['tgt']))
 elif n=='2':print("Thanks for using!")breakelse:print("The input is wrong!")if __name__=='__main__':translation()

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python realizes online translation
Python realizes online translation function
Python realizes online microblog data visualization
Python realizes spaceship war
Python realizes tank battle
Python3 realizes airplane war game
Python realizes apple eating game
Python realizes the guessing game
Python realizes 3D map visualization
Python realizes face sign-in system
Python realizes batch naming photos
Python realizes business card management system
Python3 realizes business card management system
Python realizes express price query system
Python realizes image recognition car function
Python realizes udp transmission picture function
Python realizes console output color font
Python realizes batch modification of file names
python3 realizes the function of mask drawing
Python realizes the greedy snake double battle