智能摘要 AI
本文介绍了如何通过逆向分析必应翻译接口,获取其API参数并实现翻译功能。作者发现必应翻译会保留HTML字符,但效果不如谷歌翻译。通过分析目标URL和请求参数,提取了表单和查询字符串中的关键参数,如`token`、`key`和`IG`,并提供了Python代码实现翻译功能。最终成功调用API并获取翻译结果。
0x00 前言
最近在做翻译接口的时候,偶然间发现必应翻译是会保留HTML字符进行翻译,但是效果还是不如谷歌翻译来的好,就算一个备用API接口吧
0x01 逆向
目标URL:https://www.bing.com/translator?setmkt=zh-cn&setlang=zh-cn
相关API接口所需参数如图所示,表单参数如下:
- fromLang: en
- to: zh-Hans
- text: how are you
- tryFetchingGenderDebiasedTranslations: true
- token: GzahiqpKZ5NVwTzZX2HMUaSgd43eBMxQ
- key: 1738936318486
查询字符串参数如下:
- isVertical: 1
- IG: 2CD61A563F1E4AFC99EBAD61310D84FF
- IID: translator.5026
IG为一个随机的MD5值,作为全局变量存储:_G.IG,如图所示
每次都会变动,与13位的时间戳绑定相关,如下图所示:
0x02 相关代码
很好,所有内容可以明文获取,也不藏着捏着,直接给出相关的Python代码:
import re
import requests
def get_bing_translation(token, key, IG, text, from_lang='en', to_lang='zh-Hans'):
# 构建POST请求的payload,确保URL编码
payload = f"fromLang={from_lang}&to={to_lang}&text={requests.utils.quote(text)}&tryFetchingGenderDebiasedTranslations=true&token={token}&key={key}"
# 构建请求头部
headers = {
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Accept': '*/*',
'Host': 'www.bing.com',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
}
# 发送POST请求
url = f"https://www.bing.com/ttranslatev3?isVertical=1&IG={IG}&IID=translator.5026"
response = requests.post(url, headers=headers, data=payload)
# 判断请求是否成功
if response.status_code == 200:
result = response.json()
return result
else:
return f"Error: {response.status_code}"
def extract_bing_translation_params():
# 发送GET请求获取网页内容
url = "https://www.bing.com/translator?mkt=zh-CN"
response = requests.get(url)
# 检查请求是否成功
if response.status_code != 200:
return f"Error: Unable to access {url}"
# 查找并提取 params_AbusePreventionHelper 数组
key_pattern = re.compile(r'params_AbusePreventionHelper\s*=\s*\[([^\]]+)\]')
key_match = key_pattern.search(response.text)
if not key_match:
return "Error: Unable to find params_AbusePreventionHelper"
# 提取并解析数组
params_str = key_match.group(1)
params_list = [item.strip().strip('"') if '"' in item else int(item) for item in params_str.split(',')]
# 查找并提取 IG 参数
ig_pattern = re.compile(r'IG:"([^"]+)"') # 强制匹配大小写的32个字符
ig_match = ig_pattern.search(response.text)
if not ig_match:
return "Error: Unable to find IG parameter"
# 提取并返回所需的参数
token = params_list[1]
key = params_list[0]
ig = ig_match.group(1)
return token, key, ig
# 示例URL
params = extract_bing_translation_params()
# 输出结果
if isinstance(params, tuple): # 确保返回的是有效的元组
# 翻译内容
text = "how are you?"
# 获取翻译
translation_result = get_bing_translation(params[0], params[1], params[2], text)
print(translation_result[0]['translations'][0]['text'])
print("Extracted Parameters:")
print(f"Token: {params[0]}")
print(f"Key: {params[1]}")
print(f"IG: {params[2]}")
else:
print(params) # 打印错误信息
收工!





评论 (0)