copytranslator
:DeepL翻译
和有道翻译
来尝试破解。DeepL翻译
网址:https://www.deepl.com/translatorDeepLy
翻译宣称是全世界最好的机器翻译。
https://www2.deepl.com/jsonrpc
,提交的参数为:id
这个参数经过我的测试,随便填一个数都可以。import timeimport randomimport requestsdef deepl_translator(sentence): sentence = '"' + sentence + '"' u_sentence = sentence.encode("unicode_escape").decode() data = '{"jsonrpc":"2.0","method": "LMT_handle_jobs","params":{"jobs":[{"kind":"default","raw_en_sentence":' + sentence + ',"raw_en_context_before":[],"raw_en_context_after":[],"preferred_num_beams":4,"quality":"fast"}],"lang":{"user_preferred_langs":["EN","ZH"],"source_lang_user_selected":"auto","target_lang":"EN"},"priority":-1,"commonJobParams":{},"timestamp":' + str( int(time.time() * 10000)) + '},"id":' + str( random.randint(1, 100000000)) + '}' r = requests.post('https://www2.deepl.com/jsonrpc', headers={'content-type': 'application/json'}, data=data.encode()) return r.json()['result']['translations'][0]['beams'] print(deepl_translator('摸鱼就开心'))# 输出:# [{'postprocessed_sentence': "I'm happy when I touch the fish.", 'num_symbols': 12},# {'postprocessed_sentence': "You'll be happy if you touch the fish.", 'num_symbols': 13},# {'postprocessed_sentence': "It's fun to touch fish.", 'num_symbols': 10},# {'postprocessed_sentence': "You'll be happy if you touch the fish", 'num_symbols': 12}]
返回的是最佳的四个翻译,num_symbols
的含义我不是很确定,猜测是代表置信度。
有道翻译
网址:http://fanyi.youdao.com/在有道翻译中同样翻译“摸鱼很快乐”这句话:
from
、to
、smartresult
、client
、doctype
、version
、keyfrom
、action
都是固定参数,不用调整。需要获取的参数:i
:输入salt
、lts
:很像时间戳,但是位数不同sign
、bv
:长度都是32位,很可能是MD5
加密以后得到的值
全局搜索salt
参数:全局搜索格式化这个js文件后,在文件内再次搜索salt
,定位到这里:
e
为输入的句子navigator.appVersion
其实就是User-Agent
,t
为它进行md5
加密后的结果
r
为当前时间的时间戳i
为r
在末尾加上一个随机整数sign
为e
和i
拼接后再分别在前后加上一个固定字符串,再进行md5加密后的结果
然后就能用Python还原整个加密过程了。
import timeimport requestsfrom hashlib import md5def youdao_translator(sentence): # 获取参数 lts = str(int(time.time() * 1000)) salt = str(int(time.time() * 10000)) ua = '5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36' bv = md5(ua.encode()).hexdigest() sign = md5(('fanyideskweb' + sentence + salt + ']BjuETDhU)zqSxf-=B#7m').encode()).hexdigest() # 创建一个会话来获取cookie s = requests.session() s.get('http://fanyi.youdao.com') # headers中必要的三个参数,其他的都不必要 headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': 'http://fanyi.youdao.com/', } data = { 'i': sentence, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': salt, 'sign': sign, 'lts': lts, 'bv': bv, 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTlME' } r = s.post('http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule', headers=headers, data=data) return r.json()['translateResult'][0][0]['tgt'] print(youdao_translator('不想学习'))# 输出:# "Don't want to learn"
精彩评论