1——zh-en翻译
2——Python来实现
# author: mmm
# datetime:2022/6/24 17:40周五
# software: PyCharm
"""
文件说明:
"""
import requests
from lxml import etree
import pandas as pd
import json
# 接受多个输入信息,以0作为结束标志
while True:
content = input('请输入您要翻译的信息:')
if content == '0':
break
else:
ua = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
parms = {
'i': content,
'from': 'AUTO',
'to': 'AUTO',
'doctype': 'json',
'client': 'fanyideskweb',
'version': 2.1,
'keyfrom': 'fanyi.web',
}
res = requests.post(url, data=parms, headers=ua)
data1 = json.loads(res.text)
a = data1['translateResult'][0][0]['src']
b = data1['translateResult'][0][0]["tgt"]
print("翻译结果:", b)
data = pd.DataFrame({'输入': [a], "输出": [b]})
data.to_csv("有道翻译.csv", mode="a", encoding="utf-8", index=False, header=0)