我真的爱发明 发表于 2022-12-26 20:47

采用抓包的方式逆向获得谷歌翻译的API

# 最开始的尝试

- 谷歌的翻译API老是发生变化,我们需要自己动手来找到谷歌的翻译API,这样才是最稳妥的解决方法

- 首先,用谷歌浏览器打开谷歌的翻译界面,看一下它是怎么请求数据的

- ![](https://remnote-user-data.s3.amazonaws.com/jLCy9m56JkM5B16e5GuY8VKALo6BT29is3Sdo7e-iZd3F5-fX2eCfECHS3oW_mRUHFz_ALMqXHpWFGcWgTLX1VlrRyJVRcSKIWRXuqBHn1wpz_Ybx3WdrECmu8axTFw0.png)

- 右键检查,进入开发者工具,选择网络(network),如图所示

- ![](https://remnote-user-data.s3.amazonaws.com/gkCcFR01G8eI5Awu1aX6UirWUkyU_sLLHX2FpLflaIu0znkG6VGZRufbUue_WM9ZEVQ9PkhldK__HgiVUGotdvqohD4NdFrIdklEW_WRBZ4gyRdaxYSNhWV1Z9SmKSIc.png)

- 点击clear清除,把之前所有的请求视觉上清空,方便我们后续查找真正的API请求

- ![](https://remnote-user-data.s3.amazonaws.com/PLZ5Cyk1mQDQeVb5giDHOEHgpC8qJBbK2oXRtoStFk5Rt0wn6X68Kez5RfW4HXgpWnMD6fuQ_CUp_AQE0g4H35HaplmfDo6RW_9QJ-xADeXReqTPaPBOJvOd07snDSlW.png)
#

- ![](https://remnote-user-data.s3.amazonaws.com/0SNvK8VWcAGKv1IqthhbworcQFpUIgLnTtts3TUtQZj9hCTpwNCffwiOcsA7p_t3_OTm18yP8C27p-tcACfQEinIHCMx_cAglYJh-syzaALE9Ynbz8uST5HX28L4Z2eh.png)
#

- 谷歌翻译大约每秒会请求一次,我们很快就可以得到谷歌服务器返回给我们的翻译结果,经过排查,下面的这个batchexecute开头的请求就是我们需要的,

- ![](https://remnote-user-data.s3.amazonaws.com/gvq3hOfn0E4_SFT9xAiZWBk7Roup1lJpMr3npT0PBYK8E3_9ZJA97I3-oGMkbTYuapVTGaa9GUymXZdAiHw7Gah8BjTUDStUZdGJNzG9hF0V26U3itl1hJfsxijqoyzL.png)
#

- 我们点开对其进行进一步分析,可以看到这是一个post的请求

- ![](https://remnote-user-data.s3.amazonaws.com/ckawQEMT2LakRLlvAvgEXcBtznnUhs0AxndhH6GYoY3uPLeuzcQACtyed0z6ylL7wYNVylBR2pKpqOm9Yj8Pb7jLmbdbbgJ21GGnc17G1b7RYXjXp-1AJGwoSbCesyrh.png)

- 从载荷中可以看到发送post请求时使用的参数,是一个表单数据

- ![](https://remnote-user-data.s3.amazonaws.com/-kXRjaVP6Jx_IJ38IwFIiVO8NxnK_mC_KKZxNVpGtE2COamiv_1xINrHh6dNX5KpaTEhdgmkUwSjAxeH2wU1gKJVDOamzG53UajaTZUT1FNmetmjhaEHUWZ822Of4knm.png)
#
#

- 从预览中可以看到翻译的结果,是一个JSON格式的数据

- ![](https://remnote-user-data.s3.amazonaws.com/8nZdl82_iSnITz7NWYUxwB8L1CR74yEUw9TtM9DaqnVhn-vxy4C1m09_BJxHg3wZZVptJwO-HxN4-VgVk5eznnuMZT62Ikoih4f71R4Z1bWBS-cT4qag4YPE8Mf9IbGd.png)

-

- # 2022.12.26

- 上面的方法是适用的,只不过是现在这个接口没有以前好找了,我们使用谷歌搜索引擎上提供的谷歌翻译来找这个接口

- 我们先在网页端测试一个翻译,可以看到这个结果反馈回来了

- ![](https://remnote-user-data.s3.amazonaws.com/Gkn93AOcIHiPTUqAfRVtmQofOn7hB-6P4M2K9HaoypOn0nJRaRNT4NYGMb5dkfh3iKmTZksyW1djb4nn1X2WGHwDxJuds_36dl1t0cb_QE8tjTwepz5oYrqn7_uPaAcX.png)

- 这是一个post请求,自己下一个网址发送自己需要翻译的文本,服务器就会给自己反馈回来对应的结果
#

# 谷歌翻译API相关信息

## 发送网址

```
### https://www.google.com/async/translate?vet=12ahUKEwjp-9mwmZf8AhXT0GEKHQc7Cs8QqDh6BAgFECw..i&ei=6YepY6njDdOhhwOH9qj4DA&yv=3&cs=0
```

- 这个网址虽然很长,但核心部分应该只是

```
### https://www.google.com/async/translate
```

- 后面的参数都是没有太大用处的,不过我们这里先做保留,之后再做精简验证
- ![](https://remnote-user-data.s3.amazonaws.com/LNP89gBXcUqJrH0EMwr_1jI64MoKySztyq4-aPdKijEyzsVDUCQVCNQm-4En9Nxk3gBI3uP8UjuKQXdLO4VWWSvs-6SpEqc3kn5BCoa9UXcZDDh2p9q-eRQNbPiEK9Hi.png)
###

## 提交的数据

```
### async=translate,sl:zh-TW,tl:zh-CN,st:1111,id:1672054875193,qc:true,ac:true,_id:tw-async-translate,_pms:s,_fmt:pc
```

###

- ![](https://remnote-user-data.s3.amazonaws.com/PqxHaKrNo38jx0e3SfW7Kl5MWDyqyYP5vzXdWCM956jqZbg95ZiGXDUmkruRJdKwnVXwKa8nTbfr5UJtKL7MF1PrfSOKfJe8A4jxsR6kn8psfXK5kJBAnuBNY94N2w1P.png)
##
- 使用postman构造一个post的请求
- ![](https://remnote-user-data.s3.amazonaws.com/rlqvNRQM_WwOE5PUsRyxYpSzfcPg3Hg-EiZNk-XScAY7gl6byTA3L2H-aCRNJxw-UcReHsinPt5BtINGdo8Zg-5308QFbUaPUW8eL100N3dArnVxZ8yyZNbAtXKhx6a9.png)

# 不过不出意外的失败了

- 返回的是一个404的网页,而不是自己想要的翻译结果,
##
- 我猜测应该是没有添加cookie的关系,我们在参数里面添加一下cookie
- 但没想到添加cookie之后依然失败
- ![](https://remnote-user-data.s3.amazonaws.com/Em-tQNMCPN-V6KAbBLqM6PoPT0e1YNPobIQ2BihXEF8MSqm3rZjukCsrsqZfJG_oUzChrx289Sku39I7qBxj0WPS24Jjjvlw-9Q7_W98aS-0kkD3PX0_uX6YYz5fM_oU.png)
#

# 实验

## 去掉参数

- 将所有的参数去掉之后,发现仍然可以正常得到结果
![](https://remnote-user-data.s3.amazonaws.com/2nwc4ikh6IlzSkJpgAXbSi5b-Ob9f21mu55EUrmvvsVjEjCTvCiy-VLj0Wb8qxnqKjkVMnjTmHZ2MsikFnlWlMZGOrVDzEoP-KyeGqLd4h30ndhqT5FdKizCRodl0RKM.png)

## 去掉Headers

- 去掉Content-Length之后就无法返回结果了 #设置的headers参数不够
![](https://remnote-user-data.s3.amazonaws.com/IzoOr9AXc53jvNrqYN9UL0_7hRXcBQ22eJ4BxxXn9hcciUyUEGExdP9rM52eA0aOJ_6gI9kDvrokkbYdNVKZXhdeRTdxizX_dkSjJrZzDo_pSRsUV9O9zffOwkSgcb3z.png)

- 结论:headers里面的参数应该尽可能多一点,最好把原请求中的所有参数都带过来
- 写成对应的Python代码

### 代码

```
import requests

url = "https://www.google.com.hk/async/translate"

payload = "async=translate,sl:en,tl:zh-CN,st:1111,id:1672056488960,qc:true,ac:true,_id:tw-async-translate,_pms:s,_fmt:pc"
headers = {
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
'DNT': '1',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-full-version': '"108.0.5359.125"',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'sec-ch-ua-platform-version': '"10.0.0"',
'sec-ch-ua-full-version-list': '"Not?A_Brand";v="8.0.0.0", "Chromium";v="108.0.5359.125", "Google Chrome";v="108.0.5359.125"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-model': '',
'sec-ch-ua-wow64': '?0',
'sec-ch-ua-platform': '"Windows"',
'Accept': '*/*',
'X-Client-Data': 'CKW1yQEIhbbJAQiktskBCMS2yQEIqZ3KAQjb08oBCLD+ygEIlaHLAQjv8swBCN75zAEI5PrMAQjxgM0BCLKCzQEI7ILNAQjIhM0BCO+EzQEIt4XNAQ==',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'host': 'www.google.com.hk',
'Cookie': '1P_JAR=2022-12-26-12; NID=511=eVLI1bG9nhyOZtqU14JBHm5Be00epdxfR4XmfQeehYyIkzgpXi6dbpNY75ZMVyS7aOjoM2oZ5WdoR8eNq6wi1-e_J0NeoyI0dtsHW-_8Ik4PGrqvuGHdcvVC03zTOEK2TY1FZL85Wimo_ZPIE3hGIrmGPSiel6-rRRW9lD30UPs'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```

- 可以正常返回我们现在的结果,
- ![](https://remnote-user-data.s3.amazonaws.com/xvZrU8Xq-l1dghGiosFoKGvnrvTfRXXQ4ljY2_nEFDS2DrcgqubDuPCupajjQCkvo2kkLVQ3I5AfYIsE7g2JP16-PlHuQ_R3vNe5l1LrrCtlZJ6Ayko0yCJSwJO07wAx.png)
- 但这个结果还不是我们想要的,我们需要对其进行一并的解析

## 对返回结果进行解析

- 其实也很好定位,就是找到` <span id="tw-answ-target-text">` 和` </span>` 之间的文本即可
![](https://remnote-user-data.s3.amazonaws.com/Qm9xH7mAOvlLaoOeLYh7rzVsk055v63juiBW_kMvg4qvBHJJk7k6rJNXGpiXPeE5W7plbk9eJFqDuRzcB7ufgbSl7pE2JHINlyKN7G9T-Mhvb9uK_syy0Ijoe831L5Ch.png)
- 提取完之后非常nice
![](https://remnote-user-data.s3.amazonaws.com/GgIC5SXDWOo-QgxtFXNyS-Foo5K-AFjPSkaFwQ2dPxpkeVrw2jVhycob9R45M9jsQC7gkwbXTZvv46JOzamd8B_MMxwtpg3hBjbiH-gZVvK7ZoAN1PHXIkqvWIUqiWuO.png)

### 完整代码

```
def Google_Translate(origin_string):
import requests

url = "https://www.google.com.hk/async/translate"

payload = "async=translate,sl:en,tl:zh-CN,st:{},id:1672056488960,qc:true,ac:true,_id:tw-async-translate,_pms:s,_fmt:pc".format(origin_string)
headers = {
    'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
    'DNT': '1',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'sec-ch-ua-arch': '"x86"',
    'sec-ch-ua-full-version': '"108.0.5359.125"',
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    'sec-ch-ua-platform-version': '"10.0.0"',
    'sec-ch-ua-full-version-list': '"Not?A_Brand";v="8.0.0.0", "Chromium";v="108.0.5359.125", "Google Chrome";v="108.0.5359.125"',
    'sec-ch-ua-bitness': '"64"',
    'sec-ch-ua-model': '',
    'sec-ch-ua-wow64': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'Accept': '*/*',
    'X-Client-Data': 'CKW1yQEIhbbJAQiktskBCMS2yQEIqZ3KAQjb08oBCLD+ygEIlaHLAQjv8swBCN75zAEI5PrMAQjxgM0BCLKCzQEI7ILNAQjIhM0BCO+EzQEIt4XNAQ==',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'host': 'www.google.com.hk',
    'Cookie': '1P_JAR=2022-12-26-12; NID=511=eVLI1bG9nhyOZtqU14JBHm5Be00epdxfR4XmfQeehYyIkzgpXi6dbpNY75ZMVyS7aOjoM2oZ5WdoR8eNq6wi1-e_J0NeoyI0dtsHW-_8Ik4PGrqvuGHdcvVC03zTOEK2TY1FZL85Wimo_ZPIE3hGIrmGPSiel6-rRRW9lD30UPs'
}

response = requests.request("POST", url, headers=headers, data=payload)

def find_string_between_A_amd_B(string, string_A, string_B):# 查找两段字符串之间的字符
    import re

    regular = '{}(.*?){}'.format(string_A, string_B)
    result = re.findall(regular, string)
    return result

result = find_string_between_A_amd_B(response.text, '', '')
return result


result = Google_Translate('222')
print("result:", result)
```

![](https://remnote-user-data.s3.amazonaws.com/bvCUzT_ql6vxh_O5wr_CXGhBorUI0n8UlYhJx1VLS-XW3X6qHMa-huvzp3g1I__qSzJ5zJWDZFOFycMi-zQTXDOGITxBgopGJhQ7eFxa7xiZYJ19Ih9Oq8fnt2kLr1vc.png)
![](https://remnote-user-data.s3.amazonaws.com/1lexzExB0C1KHzm_jkO7evd5fn7TwbkPJl38OJmlcsipjcGgWEiJP_rfPN-pq_sRMs1tQx1ac85yboSFXss9fYSlwyVgI7ycftejtyCUYmHnPINRPMqUPQZwTLbdcDdw.png)

#

我真的爱发明 发表于 2023-2-27 18:20

baojun1582 发表于 2023-2-27 16:34
这个不需要抠JS吗

这个不需要,简单的逆向即可

upseem 发表于 2024-1-5 14:59



感谢你的代码,不过我试了试最后一段代码解析字符串有些乱码,我也给出一段可以用的代码,在您的基础上加了一下

import requests
from bs4 import BeautifulSoup

url = "https://www.google.com.hk/async/translate"

payload = "async=translate,sl:en,tl:zh-CN,st:who are you,id:1672056488960,qc:true,ac:true,_id:tw-async-translate,_pms:s,_fmt:pc"
headers = {
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
'DNT': '1',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-full-version': '"108.0.5359.125"',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'sec-ch-ua-platform-version': '"10.0.0"',
'sec-ch-ua-full-version-list': '"Not?A_Brand";v="8.0.0.0", "Chromium";v="108.0.5359.125", "Google Chrome";v="108.0.5359.125"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-model': '',
'sec-ch-ua-wow64': '?0',
'sec-ch-ua-platform': '"Windows"',
'Accept': '*/*',
'X-Client-Data': 'CKW1yQEIhbbJAQiktskBCMS2yQEIqZ3KAQjb08oBCLD+ygEIlaHLAQjv8swBCN75zAEI5PrMAQjxgM0BCLKCzQEI7ILNAQjIhM0BCO+EzQEIt4XNAQ==',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'host': 'www.google.com.hk',
'Cookie': '1P_JAR=2022-12-26-12; NID=511=eVLI1bG9nhyOZtqU14JBHm5Be00epdxfR4XmfQeehYyIkzgpXi6dbpNY75ZMVyS7aOjoM2oZ5WdoR8eNq6wi1-e_J0NeoyI0dtsHW-_8Ik4PGrqvuGHdcvVC03zTOEK2TY1FZL85Wimo_ZPIE3hGIrmGPSiel6-rRRW9lD30UPs'
}

def extract_translated_text(html):
    soup = BeautifulSoup(html, 'html.parser')
    target_text = soup.find('span', {'id': 'tw-answ-target-text'})
    if target_text:
      return target_text.get_text()
    return None



response = requests.request("POST", url, headers=headers, data=payload)

result = extract_translated_text(response.text,)
print(result)




python translate.py   
你是谁

lsy832 发表于 2022-12-26 20:54

新的思路啊

okmad 发表于 2022-12-26 20:57

感谢大佬分享,这东西怎么在火车头调用?

kotlyne 发表于 2022-12-26 21:02

感谢分享,学习了

li083m 发表于 2022-12-26 21:11

学习到了

排骨的自白 发表于 2022-12-26 21:57

谷歌有相关信息

我真的爱发明 发表于 2022-12-26 22:03

排骨的自白 发表于 2022-12-26 21:57
谷歌有相关信息

具体说一下

yzxqhdx 发表于 2022-12-26 22:04

貌似没啥用。。

luxingyu329 发表于 2022-12-26 22:20

现在能打开谷歌翻译吗?

oxding 发表于 2022-12-26 22:25

import re

    regular = '{}(.*?){}'.format(string_A, string_B)
    result = re.findall(regular, string)
    return result

result = find_string_between_A_amd_B(response.text, '', '')
return result

这几段代码是什么意思?
页: [1] 2 3 4 5 6 7 8 9
查看完整版本: 采用抓包的方式逆向获得谷歌翻译的API