|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import urllib.request
import urllib.parse
import json
import time
import random
import hashlib
#生成md5字符串
def getMd5(value):
md5 = hashlib.md5()
md5.update(bytes(value,encoding='utf-8'))
md5_str = md5.hexdigest()
return md5_str
#输入需要翻译的文字
content = input('请输入需要翻译的句子:')
#生成salt值
r = str(int(time.time()*1000))
salt = r + str(int(random.random()*10))
#生成sign
value = "fanyideskweb" + content + salt + "@6f#X3=cCuncYssPsuRUE"
md5_str = getMd5(value)
head = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Length': str(233+len(content)),
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; OUTFOX_SEARCH_USER_ID=506260966@223.107.129.197; JSESSIONID=abcYjeQ-CFnD_dLfPaifx; OUTFOX_SEARCH_USER_ID_NCOO=2052442007.1493692; ___rl__test__cookies=1586058282296',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/?keyfrom=dict2.index',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = salt
data['sign'] = md5_str
data['ts'] = r
data['bv'] = '70244e0061db49a9ee62d341c5fed82a'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
|
|