1. 程式人生 > 其它 >有道詞典

有道詞典

一、分析

1、資料不在頁面上,猜測進行js請求

2、對js進行查詢

3、對比

``
i: dog
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 16361336785104
sign: 07f8b61a6f0174e3629fe5d58eca26a9
lts: 1636133678510
bv: 4a2c81385a7f2e299f7b17973e387445
doctype: json
version: 2.1
keyfrom: fanyi.web
action: FY_BY_REALTlME

i: cat
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 16361337450419
sign: 9963acc8b87ca11ee33bc7a9ea8f6b74
lts: 1636133745041
bv: 4a2c81385a7f2e299f7b17973e387445
doctype: json
version: 2.1
keyfrom: fanyi.web
action: FY_BY_REALTlME

``

其中只有salt、sign、lts不同其他的都相同

4、定位

5、js程式碼轉換為python

時間戳

import time
print(int(time.time())

md5加密

from hashlib import md5

str = "123456"
md = md5()   #獲取一個md5加密演算法物件
md.update(str.encode())  #制定需要加密的字串
res = md.hexdigest()  #獲取加密後的16進位制字串
print(res)

隨機數

import random
 
print(random.randint(0,9))  # 生成 0 ~ 9 之間的隨機數

6、salt和sign用程式碼寫出來

二、程式碼

from hashlib import md5
import random
import time
import requests

url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"

headers = {
	
	"Cookie": "[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=1595983331.8910015; _ga=GA1.2.1831723733.1622359908; JSESSIONID=aaabIn-Rp5GG57Q1TDZZx; ___rl__test__cookies=1636172237405",
	
	"Referer": "https://fanyi.youdao.com/",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40",
	
}


word = input("請輸入要翻譯的單詞:")
ts = str(int(time.time()*1000))
salt = ts + str(random.randint(0,9))
str_ = "fanyideskweb" + word + salt + "Y2FYu%TNSbMCxc3t2u^XT"

md = md5()
md.update(str_.encode())  #制定需要加密的字串	
sign = md.hexdigest()  #獲取加密後的16進位制字串

data = {
  'i': word ,
  'from':'AUTO',
  'to': 'AUTO',
  'smartresult': 'dict',
  'client': 'fanyideskweb',
  'salt': salt,
  'sign': sign ,
  'lts': ts ,
  'bv': '4a2c81385a7f2e299f7b17973e387445',
  'doctype': 'json',
  'version': '2.1',
  'keyfrom': 'fanyi.web',
  'action': 'FY_BY_REALTlME'
}

html = requests.post(url = url,data = data,headers = headers)
print(html.text)

html.close()