python实现智能翻译机
需要用到的库和及其导入如下:
1、from tkinter import *
2、import requests
3、import json
代码实现如下:
#!/usr/bin/env python
-*- coding:utf-8 -*-
from tkinter import *
import requests
import json
def translate():
content=entry1.get().strip()#去除空格
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom='
data={
'i':content,
'doctype':'json'
}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W) Gecko/20100101 Firefox/67.0'
}
response=requests.post(url,data=data,headers=headers)
ret=response.content.decode('utf-8')#可以转码
result=json.loads(ret)
result=result['translateResult'][0][0]['tgt']