爬虫案例:有道翻译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': '-815609020@10.55.164.249',
    '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': 'OUTFOX_SEARCH_USER_ID=-815609020@10.55.164.249; 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

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

相关推荐
_OP_CHEN2 小时前
【测试理论与实践】(九)Selenium 自动化测试常用函数全攻略:从元素定位到文件上传,覆盖 99% 实战场景
自动化测试·python·测试开发·selenium·测试工具·测试工程师·自动化工具
我的xiaodoujiao4 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 38--Allure 测试报告
python·学习·测试工具·pytest
Boilermaker199210 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
沈浩(种子思维作者)10 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
MM_MS10 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂10 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs11 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_9911 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
io_T_T11 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
古城小栈11 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust