!/usr/bin/env pyhton
--coding:utf-8--
import urllib.request
import urllib.parse
import os,sys
import json
while True:
content = input("名1:")
#passwd = input("密码:")
if content == 'q!':
break
url_interface = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link"
head = { }
head['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
data = {}
data['type'] = 'AUTO'
data['i'] = content
data['doctype'] = 'json'
# data[''] = ''
data['xmlVersion'] = '1.8'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
# data[''] = ''
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')
#req = urllib.request.Request(url_interface,data)
req = urllib.request.Request(url_interface,data,head) #或用head头
#req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
relt = json.loads(html)
#print(relt)
result = relt['translateResult'][0][0]['tgt']
print(result)