爬虫案例:有道翻译python逆向

pip install

  • pip install requests
  • pip install base64
  • pip install pycrytodome

tools

浏览器的开发者工具,重点使用断点,和调用堆栈

工具网站:https://curlconverter.com/ 简便请求发送信息

flow

  1. 根据网站信息,preview,response均是加密数据,或者说请求和响应我们都需要使用代码来进行模仿
  2. 由请求方式是post,所以我们需要关注payload载荷发现动态值sign,mysticTime是时间戳
  3. 对JS代码进行分析,找到动态值生成的地方,这里可以使用initiator发起程序,使用调用堆栈进行溯源,打断点进行测试,分析可知动态值是由md5摘要得出。
  4. 分析可知数据是base64变种对响应数据进行base64解码处理,分析加密算法是AES对称加密,找到对称密钥key,iv,进行解密。
  5. 最后对json数据进行反序列化,取得特定值

Code

python 复制代码
import time
import hashlib
import base64
import json
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

def get_md5(value, is_hex=True):
    """
    md5 abstract algorithm

    params:
        value: str, the value to be md5
        is_hex: bool, whether to return the md5 value in hex format
         
    return: str, the md5 value
    """
    md5 = hashlib.md5()
    md5.update(value.encode('utf-8'))
    if is_hex:
        return md5.hexdigest()
    else:
        return md5.digest()
    
word = input("请输入要翻译的单词:")
url =  'https://dict.youdao.com/webtranslate'
mysticTime = str(int(time.time() * 1000))

# (1)构建逆向动态值
d = 'fanyideskweb'
e = mysticTime
u = 'webfanyi'
t = 'fsdsogkndfokasodnaso'

i = f"client={d}&mysticTime={e}&product={u}&key={t}"
sign = get_md5(i)

# (2)请求模拟
cookies = {
    'OUTFOX_SEARCH_USER_ID': '[email protected]',
    'OUTFOX_SEARCH_USER_ID_NCOO': '1719344943.4114175',
    '_ga': 'GA1.2.674292823.1712131832',
}

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': '[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=1719344943.4114175; _ga=GA1.2.674292823.1712131832',
    'Origin': 'https://fanyi.youdao.com',
    'Referer': 'https://fanyi.youdao.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
    'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'referer': 'https://fanyi.youdao.com/',
}

data = {
    'i': word,
    'from': 'auto',
    'to': '',
    'useTerm': 'false',
    'dictResult': 'true',
    'keyid': 'webfanyi',
    'sign': sign,
    'client': 'fanyideskweb',
    'product': 'webfanyi',
    'appVersion': '1.0.0',
    'vendor': 'web',
    'pointParam': 'client,mysticTime,product',
    'mysticTime': mysticTime,
    'keyfrom': 'fanyi.web',
    'mid': '1',
    'screen': '1',
    'model': '1',
    'network': 'wifi',
    'abtest': '0',
    'yduuid': 'abcdefg',
}

res = requests.post(url , cookies=cookies, headers=headers, data=data)


# base64变种 --> 正常base64
res_encrypt_base64 = res.text.replace('-', '+').replace('_', '/')


# (3)解码和解密数据
res_encrypt_bytes = res_encrypt_base64.encode('utf-8')


# 1.解码  
res_encrypt_decode = base64.b64decode(res_encrypt_bytes)


# 2.解密
t = 'ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl'
o = 'ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4'

key = get_md5(t, is_hex=False)
iv = get_md5(o, is_hex=False)

aes = AES.new(key, AES.MODE_CBC, iv)
source_data = aes.decrypt(res_encrypt_decode)

# print(repr(source_data)):json字符串,有base64填充,所以需要去填充
data = unpad(source_data, 16)  # source_data是bytes类型,需要先解码
data = json.loads(data)
print(data['translateResult'][0][0]['tgt'])

END

我的想法:想要实现的功能是结合其他爬虫程序,进行批量的文章翻译自动化,但是这个爬虫脚本有点鸡肋,他对长文本的内容不能很好的翻译。

相关推荐
iCxhust2 分钟前
Prj10--8088单板机C语言8259测试(1)
c语言·开发语言
крон3 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan4 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊4 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1184 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之5 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?5 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头5 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
lyaihao5 小时前
使用python实现奔跑的线条效果
python·绘图
liuyang-neu6 小时前
java内存模型JMM
java·开发语言