python版有道翻译,需要用到的模块:
Urllib
time
hashlib
random
json
#encoding:utf-8
#__Author__ = Search__
#_PlugName_ = YouDaoDict
import urllib.request
import urllib.parse
import time
import random
import hashlib
import json
headers = {}
headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.7.0.16013'
timestamp = int(time.time() * 1000) + random.randint(0,10)
content = input('请输入您需要翻译的内容:')
u = "fanyideskweb"
d = content
f = str(timestamp)
c = "rY0D^0'nM0}g5Mm1z%1G4"
sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
data = {
'i': content,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': timestamp,
'sign': sign,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICK',
'typoResult': 'true'
}
request = urllib.request.Request(url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=https://www.google.com/',method='POST',data=data,headers=headers)
result_str = response.read().decode('utf-8')
result_dict = json.loads(result_str)
print (result_dict["translateResult"][0][0]['tgt'])
Pyhon3基本自带. 没有的同学可以通过pip安装:
如有侵权,请联系小编删除
领取专属 10元无门槛券
私享最新 技术干货