标签:real utf-8 abc 字符 web 翻译 xmlhttp like apple
思路:
背景,自己想做个词典,但是没有庞大词库支持,那就借用某道的吧
-------分割线-------
通过在有道翻译上的查词分析出它是通过post请求而且是ajax异步处理,那么找到请求信息,获取表单数据,找到发送Ajax的js,拼出需要的字段信息,之后……恭喜你,成功了。(●‘?‘●)
源码:
1 import json 2 from get_and_post import post 3 # md5加密 4 def md5(kw,salt): 5 import hashlib 6 # 创建md5对象 7 m = hashlib.md5() 8 # 生成加密字符串 9 # js源码:o = u.md5(S + n + r + D) 10 # S = "fanyideskweb" 11 # n = kw 12 # r = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10)) 13 # D = "ebSeFb%=XZ%T[KZ)c(sy!" 14 m.update(("fanyideskweb" + kw + salt + "ebSeFb%=XZ%T[KZ)c(sy!").encode(‘utf-8‘)) 15 # 获取加密后的字符串 16 sign = m.hexdigest() 17 return sign 18 19 20 def youdao(kw): 21 url = ‘http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule‘ 22 # 定义salt 23 # r = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10)) 24 import time, random 25 salt = str(int(time.time() * 1000) + random.randint(0,10)) 26 sign = md5(kw, salt) 27 form = { 28 "i": kw, 29 "from": "AUTO", 30 "to": "AUTO", 31 "smartresult": "dict", 32 "client": "fanyideskweb", 33 "salt": salt, 34 "sign": sign, 35 "doctype": "json", 36 "version": "2.1", 37 "keyfrom": "fanyi.web", 38 "action": "FY_BY_REALTIME", 39 "typoResult": "false", 40 } 41 headers = { 42 "Accept": "application/json, text/javascript, */*; q=0.01", 43 "Accept-Language": "zh-CN,zh;q=0.9", 44 "Connection": "keep-alive", 45 "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", 46 "Cookie": "OUTFOX_SEARCH_USER_ID=1228578818@10.169.0.83; _ntes_nnid=cfe37c34689077c73fb91832c6d23f01,1531735426782; OUTFOX_SEARCH_USER_ID_NCOO=1071254637.8014078; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcdU07ploiZ-gRehC2uw; ___rl__test__cookies=1534226607617", 47 "Host": "fanyi.youdao.com", 48 "Origin": "http://fanyi.youdao.com", 49 "Referer": "http://fanyi.youdao.com/?keyfrom=dict2.index", 50 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36", 51 "X-Requested-With": "XMLHttpRequest", 52 } 53 html_bytes = post(url,form,headers=headers) 54 translation = json.loads(html_bytes.decode(‘utf-8‘)) 55 try: 56 translation = translation[‘smartResult‘][‘entries‘][1:] 57 res = ‘‘ 58 for i in translation: 59 res += i 60 except: 61 res = translation[‘translateResult‘][0][0][‘tgt‘] 62 return res 63 64 if __name__ == ‘__main__‘: 65 kw = input(‘请输入:‘) 66 translation = youdao(kw) 67 print(translation)
标签:real utf-8 abc 字符 web 翻译 xmlhttp like apple
原文地址:https://www.cnblogs.com/zhxd-python/p/9478152.html