Go语言实践-在线词典 | 青训营

128 阅读2分钟

Go语言实践-在线词典

1.介绍

在Goland输入一个单词,命令行中会显示这个单词的发音和注释。

2.步骤

1.先抓包并找到网站并获取其发送命令的URL

彩云小译 - 在线翻译 (caiyunapp.com)

image.png 鼠标右键打开网页,点击检查

image.png 打开网络,我这里翻译为中文

image.png 点击dict发现发送的请求和要翻译的单词和翻译出来的词

注意观察是post请求

image.png

2.这个请求很复杂,直接用代码来构造很麻烦,所以我们可以用另一种简单的方式来生成请求

右键打卡dict,复制其cURL(bash)

image.png 打开网址:curlconverter.com/go/

把赋值的信息粘贴进去,并点击go复制下面生成的代码

image.png 复制代码到Goland

image.png

3.进行代码解读

1.首先了解一下json

构建结构体使其和json结构体格式相同

image.png

image.png 要对response创建结构体比较复杂 所以需要对其进行解读复制response的内容 打开这个工具网站:oktools.net/json2go image.png 生成结构体转换嵌套

type AutoGenerated struct {
	Rc int `json:"rc"`
	Wiki struct {
	} `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"`
}

经过加工最终生成

package main  
  
import (  
"bytes"  
"encoding/json"  
"fmt"  
"io"  
"log"  
"net/http"  
"os"  
)  
  
// 请求  
type DictRequest struct {  
TransType string `json:"trans_type"`  
Source string `json:"source"`  
UserID string `json:"user_id"`  
}  
  
// 响应  
type DictResponse struct {  
Rc int `json:"rc"`  
Wiki struct {  
} `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"`  
}  
  
func main() {  
//检查命令行传入的参数个数(第一个参数是程序名称本身,第二个参数是传入的单词,因此必须是两个参数)  
if len(os.Args) != 2 {  
fmt.Fprintf(os.Stderr, `usage: simpleDict WORD example: simpleDict hello`)  
os.Exit(1)  
}  
//将第二个命令行参数也就是单词传给变量 word  
word := os.Args[1]  
query(word)  
}  
  
// 将程序的主要代码封装为query()函数,传入要翻译的单词  
func query(word string) {  
client := &http.Client{}  
request := DictRequest{TransType: "en2zh", Source: word}  
buf, err := json.Marshal(request)  
if err != nil {  
log.Fatal(err)  
}  
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("authority", "api.interpreter.caiyunai.com")  
req.Header.Set("accept", "application/json, text/plain, */*")  
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")  
req.Header.Set("app-name", "xy")  
req.Header.Set("cache-control", "no-cache")  
req.Header.Set("content-type", "application/json;charset=UTF-8")  
req.Header.Set("device-id", "b72e9f6cbb97432941b8adf317a17dee")  
req.Header.Set("origin", "https://fanyi.caiyunapp.com")  
req.Header.Set("os-type", "web")  
req.Header.Set("os-version", "")  
req.Header.Set("pragma", "no-cache")  
req.Header.Set("referer", "https://fanyi.caiyunapp.com/")  
req.Header.Set("sec-ch-ua", `"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"`)  
req.Header.Set("sec-ch-ua-mobile", "?0")  
req.Header.Set("sec-ch-ua-platform", `"Windows"`)  
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/115.0.0.0 Safari/537.36")  
req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")  
resp, err := client.Do(req)  
if err != nil {  
log.Fatal(err)  
}  
defer resp.Body.Close()  
bodyText, err := io.ReadAll(resp.Body)  
if err != nil {  
log.Fatal(err)  
}  
  
//防御性编程  
if resp.StatusCode != 200 {  
log.Fatal("bad StatusCode:", resp.StatusCode, "body:", string(bodyText))  
}  
var dictResponse DictResponse  
err = json.Unmarshal(bodyText, &dictResponse)  
if err != nil {  
log.Fatal(err)  
}  
  
//fmt.Println("%#v\n", dictResponse)  
fmt.Println(word, "UK:", dictResponse.Dictionary.Prons.En,  
"US:", dictResponse.Dictionary.Prons.EnUs)  
for _, item := range dictResponse.Dictionary.Explanations {  
fmt.Println(item)  
}  
}