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