网上爬有道词典有两种主流方式,一种是简单的,一种是复杂的。翻译精度大不同。
简单的url:http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule
复杂的url:http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
简单版的写法自然很简单:
import requests,json url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' str = 'hello' form_data = { 'i':str, #请求数据需要的至少两个参数之一 'doctype': 'json' #请求数据需要的至少两个参数之二 } re = requests.post(url,form_data) print json.loads(re.text)['translateResult'][0][0]['tgt']
复杂版的写法:要求参照有道翻译页面抓包的样式。
参考写法见:https://blog.csdn.net/doraemon_meow_meow/article/details/81676172 但其中有个问题需要指明:
请求数据中的salt和sign需要一定的算法来解决,也就是这篇博文中的S和D变量,S是对的,但D变量是每个ip都不一样的,如果D变量不对,翻译结果和简单版一样,精度比较低,意思是照抄上面的博客相当于是失败的。
因此按照:https://blog.csdn.net/quickzhao3223/article/details/82627296中的方式,找到了fanyi.min.js中的加密算法,查看到自己的D变量,我的是:p09@Bn{h02_BIEe]$P^nG 。
而且D不正确,翻译长度有限制
原文: The puzzle is apocryphally attributed to a 2nd-century Chinese general, who gave the puzzle to his wife to occupy her time while he was away at war.
当D不正确,但使用复杂url的结果:这个难题是虚情假意地归因于2 nd-cen #后面没了,原文直接被截断了
当D不正确,且使用简单url的结果:拼图是公元2世纪时虚情假意地归因于中国将军,把难题交给他的妻子当他离开时,占用她的时间处于战争状态。
当D正确,但用简单版url的结果: 拼图是公元2世纪时虚情假意地归因于中国将军,把难题交给他的妻子当他离开时,占用她的时间处于战争状态。
当D正确,且用复杂版url的结果: 这个谜题被认为是一个公元2世纪的中国将军的作品,他把这个谜题给了他的妻子,让她在他外出打仗的时候打发时间。
由上可知哪种翻译更好了!!!
我的复杂版代码:
# -*- coding:utf-8 -*- import time,random import requests,json def md5_my(key,salt): import hashlib S = "fanyideskweb" D = "p09@Bn{h02_BIEe]$P^nG" need_str = S + key + salt + D md5_o = hashlib.md5() sign_bytes = need_str md5_o.update(sign_bytes) sign_str = md5_o.hexdigest() return sign_str url = r'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' key = 'The puzzle is apocryphally attributed to a 2nd-century Chinese general,who gave the puzzle to his wife to occupy her time while he was away at war.' salt = str(int(time.time() * 1000 + random.randint(0, 10))) sign_md5_str = md5_my(key,salt) data = { 'i':key, 'doctype':'json', 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': salt, 'sign': sign_md5_str, 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTIME', 'typoResult': 'false', } headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', #'Content-Length': '406', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': '[email protected]; JSESSIONID=aaaEcIuSVNrCXg8oNrpHw; __guid=204659719.2498346897558220300.1547518241172.141; monitor_count=1; OUTFOX_SEARCH_USER_ID_NCOO=65049609.31283616; ___rl__test__cookies=1547521719081', 'Host': 'fanyi.youdao.com', 'Origin': 'http://fanyi.youdao.com', 'Referer': 'http://fanyi.youdao.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', } re = requests.post(url,data,headers=headers) print json.loads(re.text)['translateResult'][0][0]['tgt']
根据这个思路,是不是可以爬一下其他词典?