逆向Bing翻译
代码示例
代码示例1
代码示例2
import requests
import re
import json
def trans(word):
flg = re.search(r'[\u4e00-\u9f5a]', word)
# en-GB 英文
# zh-Hans 中文,
# ja 日文
from_word, to_word = ('ja', 'zh-Hans') if flg else ('zh-Hans', 'ja')
uri = 'https://cn.bing.com/translator'
gi = requests.get(uri).text
ig = re.search(r'IG:"(.*?)"', gi).group(1)
token = re.search(r'params_AbusePreventionHelper = (.*?);', gi).group(1)
tokens = token.replace("[", "")
js = tokens.split(',')
t = js[1][1:33]
url = 'https://cn.bing.com/ttranslatev3?isVertical=1&&IG={}&IID=translator.5027'.format(ig)
data = {
'fromLang': from_word,
'text': word,
'to': to_word,
'token': t,
'key': js[0],
'tryFetchingGenderDebiasedTranslations': 'true'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
try:
response = requests.post(url, data=data, headers=headers)
translations = response.json()[0]['translations']
translated_text = translations[0]['text']
return translated_text
except:
return "翻译失败"
if __name__ == '__main__':
word = "你好"
result = trans(word)
print(result)
# 示例结果:You said that you want to escape, and you want to leave the hollow or not
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) # 打印错误信息