一个命令行词典,使用浏览器中的开发者工具进行抓包。拿到接口后再在 IDE 中进行实现。
抓包
打开检查后找到dict查看状态代码是否为POST,复制dict选择复制为cURL(bash)拿到接口
通过https://curlconverter.com/#go将cURL转写成GO语言代码
序列化
将我们的输入通过序列化的方式写入,这个可以通过写一个结构体来实现,使用 json.Marshal 方法实现序列化,此时输入通过字节的方式写入
type DictRequest struct {
TransType string `json:"trans_type"`
Source string `json:"source"`
}
request := DictRequest{TransType: "en2zh", Source: word}
input, err := json.Marshal(request)
if err != nil {
log.Fatal(err)
}
var data = bytes.NewReader(input)
返回值反序列化
首先确定返回值需要返回的内容及格式,在响应中可以获取到
可以按照返回格式再写一个结构体,或者直接获取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 []interface{} `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 方法得到该结构体类型的返回值,由此可以随意控制想得到的的输出格式,这里我们只需要中文解释的内容
var output dictResponse
err = json.Unmarshal(bodyText, &output)
if err != nil {
log.Fatal(err)
}
fmt.Println(word, "UK:", output.Dictionary.Prons.En, "US:", output.Dictionary.Prons.EnUs)
for _, i := range output.Dictionary.Explanations {
fmt.Println(i)
}
All Code:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type DictRequest struct {
TransType string `json:"trans_type"`
Source string `json:"source"`
}
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 []interface{} `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 query(word string) {
client := &http.Client{}
request := DictRequest{TransType: "en2zh", Source: word}
input, err := json.Marshal(request)
if err != nil {
log.Fatal(err)
}
var data = bytes.NewReader(input)
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,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
req.Header.Set("app-name", "xy")
req.Header.Set("content-type", "application/json;charset=UTF-8")
req.Header.Set("device-id", "")
req.Header.Set("origin", "https://fanyi.caiyunapp.com")
req.Header.Set("os-type", "web")
req.Header.Set("os-version", "")
req.Header.Set("referer", "https://fanyi.caiyunapp.com/")
req.Header.Set("sec-ch-ua", `"Not_A Brand";v="99", "Microsoft Edge";v="109", "Chromium";v="109"`)
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/109.0.0.0 Safari/537.36 Edg/109.0.1518.55")
req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")
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 output dictResponse
err = json.Unmarshal(bodyText, &output)
if err != nil {
log.Fatal(err)
}
fmt.Println(word, "UK:", output.Dictionary.Prons.En, "US:", output.Dictionary.Prons.EnUs)
for _, i := range output.Dictionary.Explanations {
fmt.Println(i)
}
}
func main() {
fmt.Println("input a dirct")
var word string
_, err := fmt.Scanf("%v", &word)
if err != nil {
log.Fatal(err)
return
}
query(word)
}
标签:Set,string,err,req,Header,json,命令行,GO,词典
From: https://www.cnblogs.com/Aidan347/p/17060141.html