0.参考
1.分析
1.1 输入翻译内容,手动点击“翻译”按钮
1.2 查看提交数据,通过多次提交确认变化量
1.3 Ctrl+Shift+f 全局搜索 salt 或 sign 定位到三处js代码块,通过搜索提交数据中的`version:2.1`的关键字`2.1`锁定目标代码块:salt 与时间戳相关,通过 MD5 生成
sign
1.4 在上方函数入口添加断点,再次点击“翻译”, F10开始步进调试
1.5 python 计算对比 MD5
2. 完整代码
import timeimport randomimport reimport hashlibimport requestsfrom scrapy import Selectorurl = 'http://fanyi.youdao.com/'s = requests.Session()s.headers = { 'Referer': 'http://fanyi.youdao.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}# Get cookiesr_url = s.get(url)# Get latest D for md5 from file fanyi.min.js# # sel = Selector(text=r_url.text)# url_js = sel.css('script::attr(src)').re_first('.*fanyi.min.js')# r_js = s.get(url_js)# D = re.search(r'D\s*=\s*"(.*?)"', r_js.text).group(1)# print(D)D = 'ebSeFb%=XZ%T[KZ)c(sy!'url_post = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'data = dict( # i = 'have you eaten', i = '你吃饭了吗?', # from = 'AUTO', to = 'AUTO', smartresult = 'dict', client = 'fanyideskweb', doctype = 'json', version = '2.1', keyfrom = 'fanyi.web', action = 'FY_BY_CLICKBUTTION', typoResult = 'false')salt = str(int(time.time()*1000) + random.randint(0, 9))print(salt)m = hashlib.md5()m.update((data['client']+data['i']+salt+D).encode('utf-8'))sign = m.hexdigest()print(sign)data.update({ 'from': 'AUTO', 'salt': salt, 'sign': sign})r_post = s.post(url_post, data=data)print(r_post.json())
3.运行代码