这是我参与「第三届青训营 -后端场」笔记创作活动的第3篇笔记
1、搜索彩云小译,输入任意中文,使之翻译成英文。
2、按F12打开控制台,点击Network,找到对应的dict。
3、右键选择copy,选择copy as cURL(bash),结果对应如下:
curl 'https://api.interpreter.caiyunai.com/v1/dict' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Accept-Language: zh-CN,zh;q=0.9' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H 'Origin: https://fanyi.caiyunapp.com' \
-H 'Referer: https://fanyi.caiyunapp.com/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: cross-site' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36' \
-H 'X-Authorization: token:qgemv4jr1y38jyq6vhvi' \
-H 'app-name: xy' \
-H 'device-id: ' \
-H 'os-type: web' \
-H 'os-version: ' \
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw '{"trans_type":"zh2en","source":"你好","user_id":"6275e1f15d5abb00122f283b"}' \
--compressed
进入代码生成工具<curlconverter.com/#go
将上一步获取到的copy as cURL(bash)黏贴,用go语言生成
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
client := &http.Client{}
var data = strings.NewReader(`{"trans_type":"zh2en","source":"你好","user_id":"6275e1f15d5abb00122f283b"}`)
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")
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/102.0.5005.63 Safari/537.36")
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="102", "Google Chrome";v="102"`)
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)
}
上面的代码已经完成了很多的功能,打印出来的bodyText是JSON格式。我们想要获得想要的翻译信息Hello,需要将JSON转化成结构体。可以使用工具JSON转Golang Struct。将控制台打印的信息粘贴到这里。
生成信息如下:
type DictResponse 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"`
}
此时只需要使用json.Unmarshal()方法将JSON转换成结构体,并用结构体.属性名的方式,找到对应的结果。
err = json.Unmarshal(bodyText, &dictResponse)
到此位置,还只能够翻译固定的中文”你好“,想要动态翻译其他中文,需要将下面一行代码中的”你好“用一个变量word代替。这样就可以翻译任何我们想要翻译的中文了。
var data = strings.NewReader(`{"trans_type":"zh2en","source":"你好","user_id":"6275e1f15d5abb00122f283b"}`)