这是我参与「第三届青训营 -后端场」笔记创作活动的第4篇笔记
1. 抓包教程
- 首先是用浏览器打开彩云小译,网址:fanyi.caiyunapp.com/#/
- 进入到网页后,按
F12,审查html - 输入good,点击翻译,找到如下的请求
- 让我们来看一下抓包到的请求的具体信息,要找到的属性页有两个
Preview和Header这两个属性页中包含了翻译网页请求接口的绝大多数信息 - 再将curl复制下来,得到一大串的字符(注意复制的时候要选
复制为curl(bash)),然后打开网址curlconverter.com/ 将刚刚的curl复制过去,转换为代码,得到
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
client := &http.Client{} //创建客户端
var data = strings.NewReader(`{"trans_type":"en2zh","source":"good"}`) //声明数据
req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) //声明请求接口
if err != nil {
log.Fatal(err)
}
//以下都是请求头
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Origin", "https://fanyi.caiyunapp.com")
req.Header.Set("Referer", "https://fanyi.caiyunapp.com/")
req.Header.Set("Sec-Fetch-Dest", "empty")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Sec-Fetch-Site", "cross-site")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39")
req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi")
req.Header.Set("app-name", "xy")
req.Header.Set("os-type", "web")
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="101", "Microsoft Edge";v="101"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
resp, err := client.Do(req) //发起请求
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //在函数结束后关闭该请求
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText)
}
这时候这段代码就可以运行了,请求主体完成,接下来我们来封装这段请求,并进入代码编写阶段
2. 代码编写
- 封装请求
func requestDict(source string) []byte {
client := &http.Client{} //创建客户端
request := requestDataInfo{
TransType: "en2zh",
Source: source,
}
buf, err := json.Marshal(request) //序列化结构体
var data = bytes.NewReader(buf)
req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) //声明请求接口
if err != nil {
log.Fatal(err)
}
//以下都是请求头
//省略
resp, err := client.Do(req) //发起请求
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //在函数结束后关闭该请求
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText) //debug
return bodyText
}
该接口返回的是请求某个单词的翻译后的json数据,那么我们的思路应该是要设置一个结构体,来接受该数据 2. 得到结构体 已知该请求返回得到的结构体非常复杂繁琐,所以需要用到结构体生成工具oktools.net/json2go,只需要…
type AutoGenerated struct { Rc int
json:"rc"Wiki struct { KnownInLaguages intjson:"known_in_laguages"Description struct { Source stringjson:"source"Target interface{}json:"target"}json:"description"ID stringjson:"id"Item struct { Source stringjson:"source"Target stringjson:"target"}json:"item"ImageURL stringjson:"image_url"IsSubject stringjson:"is_subject"Sitelink stringjson:"sitelink"}json:"wiki"Dictionary struct { Prons struct { EnUs stringjson:"en-us"En stringjson:"en"}json:"prons"Explanations []stringjson:"explanations"Synonym []stringjson:"synonym"Antonym []stringjson:"antonym"WqxExample [][]stringjson:"wqx_example"Entry stringjson:"entry"Type stringjson:"type"Related []interface{}json:"related"Source stringjson:"source"}json:"dictionary"}
3.封装函数,写人机交互,代码:
func query(word string) {
//要求获取该单词的音标、语言类型以及解释
res := requestDict(word)
fmt.Println("你查询的单词是:", word, "UK", res.Dictionary.Prons.En, "US", res.Dictionary.Prons.En)
for _, exp := range res.Dictionary.Explanations {
fmt.Println(exp)
}
}
func main() {
var word string
fmt.Scanf("%s", &word)
strings.TrimSuffix(word, "\n")
query(word)
}
3. 实战 - 实现火山翻译的抓包
根据我们第二点开始实现火山翻译的抓包:
定位到请求,我们在这个请求上开始,要拿到三个东西
1.
发送请求的包头拿到url即可
2.返回的json数据在priview
3.发送请求的数据在Request payload中
最终代码提交
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"
)
type requestDataInfoColor struct { //请求数据结构体
TransType string `json:"trans_type"`
Source string `json:"source"`
}
type requestDataInfoFireMount struct {
Text string `json:"text"`
Language string `json:"language"`
}
type dictResColor struct {
Rc int `json:"rc"`
Wiki struct {
KnownInLaguages int `json:"known_in_laguages"`
Description struct {
Source string `json:"source"`
Target interface{} `json:"target"`
} `json:"description"`
ID string `json:"id"`
Item struct {
Source string `json:"source"`
Target string `json:"target"`
} `json:"item"`
ImageURL string `json:"image_url"`
IsSubject string `json:"is_subject"`
Sitelink string `json:"sitelink"`
} `json:"wiki"`
Dictionary struct {
Prons struct {
EnUs string `json:"en-us"`
En string `json:"en"`
} `json:"prons"`
Explanations []string `json:"explanations"`
Synonym []string `json:"synonym"`
Antonym []string `json:"antonym"`
WqxExample [][]string `json:"wqx_example"`
Entry string `json:"entry"`
Type string `json:"type"`
Related []interface{} `json:"related"`
Source string `json:"source"`
} `json:"dictionary"`
}
type dictResFireMount struct {
Words []struct {
Source int `json:"source"`
Text string `json:"text"`
PosList []struct {
Type int `json:"type"`
Phonetics []struct {
Type int `json:"type"`
Text string `json:"text"`
} `json:"phonetics"`
Explanations []struct {
Text string `json:"text"`
Examples []struct {
Type int `json:"type"`
Sentences []struct {
Text string `json:"text"`
TransText string `json:"trans_text"`
} `json:"sentences"`
} `json:"examples"`
Synonyms []interface{} `json:"synonyms"`
} `json:"explanations"`
Relevancys []interface{} `json:"relevancys"`
} `json:"pos_list"`
} `json:"words"`
Phrases []interface{} `json:"phrases"`
BaseResp struct {
StatusCode int `json:"status_code"`
StatusMessage string `json:"status_message"`
} `json:"base_resp"`
}
func requestFireMount(source string, wg *sync.WaitGroup) dictResFireMount {
startTime := time.Now()
client := &http.Client{}
request := requestDataInfoFireMount{
Language: "en",
Text: source,
}
buf, err := json.Marshal(request)
var data = bytes.NewReader(buf)
//var data = strings.NewReader(`{"text":"hello\n\n","language":"en"}`)
req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVOQDaKSfvtSWqkwOizRCs0&_signature=_02B4Z6wo00001ShekuAAAIDA.5i.mxIIxsUoXpZAAChtJQ1fYIB5IufTjBM1xCFC1hk3Q36Izh43743m4lvY451AeNcLTE.xA-xHY1hWaz0vhCT12BnAM6gYjkY863HRQd.vv90dwOjP48.738", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "translate.volcengine.com")
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"`)
req.Header.Set("accept", "application/json, text/plain, */*")
req.Header.Set("content-type", "application/json")
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("origin", "https://translate.volcengine.com")
req.Header.Set("sec-fetch-site", "same-origin")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("referer", "https://translate.volcengine.com/translate?category=&home_language=zh&source_language=detect&target_language=zh&text=good%0A")
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("cookie", "x-jupiter-uuid=16521717160593380; i18next=zh-CN; s_v_web_id=verify_9ee5cfbc7da4332702124644a695e0e7; _tea_utm_cache_2018=undefined; ttcid=3bea2de0171748ff8afafa24de02c4c615; tt_scid=X-WQFPZGdMMILVTs.Vl6C-W7n6ZTe6R5rRA3.cjgNzb.7pA9LfkZnyJSOGxmrAdX3664")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var res dictResFireMount
err = json.Unmarshal(bodyText, &res)
finishTime := time.Now()
fmt.Println("火山翻译用的时间是:", finishTime.Sub(startTime))
wg.Done()
return res
}
func requestDictColor(source string, wg *sync.WaitGroup) dictResColor {
startTime := time.Now()
client := &http.Client{} //创建客户端
request := requestDataInfoColor{
TransType: "en2zh",
Source: source,
}
buf, err := json.Marshal(request) //序列化结构体
var data = bytes.NewReader(buf)
req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) //声明请求接口
if err != nil {
log.Fatal(err)
}
//以下都是请求头
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Origin", "https://fanyi.caiyunapp.com")
req.Header.Set("Referer", "https://fanyi.caiyunapp.com/")
req.Header.Set("Sec-Fetch-Dest", "empty")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Sec-Fetch-Site", "cross-site")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39")
req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi")
req.Header.Set("app-name", "xy")
req.Header.Set("os-type", "web")
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="101", "Microsoft Edge";v="101"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
resp, err := client.Do(req) //发起请求
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //在函数结束后关闭该请求
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 { //处理请求不成功的情形
log.Fatal("bad status Code:", resp.StatusCode, "body", string(bodyText))
}
finishTime := time.Now()
fmt.Println("彩云翻译用的时间是:", finishTime.Sub(startTime))
wg.Done()
var res dictResColor
err = json.Unmarshal(bodyText, &res)
return res
}
func queryColor(word string, wg *sync.WaitGroup) {
//要求获取该单词的音标、语言类型以及解释
res := requestDictColor(word, wg)
fmt.Println("你查询的单词是:", word, "UK", res.Dictionary.Prons.En, "US", res.Dictionary.Prons.En)
for _, exp := range res.Dictionary.Explanations {
fmt.Println(exp)
}
}
func queryFireMount(word string, wg *sync.WaitGroup) {
res := requestFireMount(word, wg)
for _, words := range res.Words {
for _, poslist := range words.PosList {
for _, explanations := range poslist.Explanations {
fmt.Println(explanations.Text)
}
}
}
}
func main() {
var word string
fmt.Scanf("%s", &word)
strings.TrimSuffix(word, "\n")
wg := sync.WaitGroup{}
wg.Add(2)
go queryFireMount(word, &wg)
go queryFireMount(word, &wg)
wg.Wait()
}