import urllib.request
import urllib.parse
import json
while True:
content = input("请输入你要翻译的内容:")
if content == "exit" or content == "退出":
break
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/"
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16248971446526'
data['sign'] = 'c2419f171ee58cb900aec5df720a8322'
data['lts'] = '1624897144652'
data['bv'] = '6c7a52ace17deb79388e05719a7255b8'
data['doctype'] = 'json'
data['Version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data["ue"] = 'UTF-8'
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(url,data)
html = response.read().decode("utf-8")
target = json.loads(html)
print("翻译结果:%s"%(target['translateResult'][0][0]['tgt']))
只能给你参考一下,这个是有道翻译的 |