关于抓包的代码生成,我将依次示例演示两个网站

  • 请求的代码生成
    https://curlconverter.com/go/

  • 响应的反序列化
    https://oktools.net/json2go

我们对李白的词条的人物关系尝试抓包

在这里插入图片描述

  1. 我们找到https://baike.baidu.com/starmap/api/gethumanrelationcard?lemmaId=1043&lemmaTitle=%E6%9D%8E%E7%99%BD这个URL
  2. 右键Copy -> Copy as cURL (bash) ,将该curl请求复制到https://curlconverter.com/go/
    在这里插入图片描述
  3. 选择Go标签,下面可见已经生成好Golang的请求体代码
    在这里插入图片描述
  4. 复制response中的dict到https://oktools.net/json2go
    在这里插入图片描述
    在这里插入图片描述
    右侧即为自动生成的结构体
  5. 将结构体和响应体代码整合可得到完整抓包程序
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

type Demo struct {
	Errno  int    `json:"errno"`
	Errmsg string `json:"errmsg"`
	List   []struct {
		RelationName string `json:"relationName"`
		LemmaID      int    `json:"lemmaId"`
		LemmaTitle   string `json:"lemmaTitle"`
		CoverPic     string `json:"coverPic"`
		Summary      string `json:"summary"`
	} `json:"list"`
	Info struct {
		Cnt           int    `json:"cnt"`
		RelationTitle string `json:"relationTitle"`
	} `json:"info"`
}

func main() {

	client := &http.Client{}
	req, err := http.NewRequest("GET", "https://baike.baidu.com/starmap/api/gethumanrelationcard?lemmaId=1043&lemmaTitle=%E6%9D%8E%E7%99%BD", nil)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Accept", "*/*")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Cookie", `BIDUPSID=D6816D0EBB7EFCA1D079B06803212084; PSTM=1660207890; BAIDUID=D6816D0EBB7EFCA18C0ABF5FBED0D1A7:SL=0:NR=10:FG=1; BDUSS=Tl1S0F2UFQ2U1N3d25COE5jWXRHVGJqR0JZNDlzTkVWc1FnYlo5SWZvY0FYNE5qSVFBQUFBJCQAAAAAAAAAAAEAAABfVxGeva2x39K7zPUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADSW2MA0ltjN; MCITY=-%3A; BDUSS_BFESS=Tl1S0F2UFQ2U1N3d25COE5jWXRHVGJqR0JZNDlzTkVWc1FnYlo5SWZvY0FYNE5qSVFBQUFBJCQAAAAAAAAAAAEAAABfVxGeva2x39K7zPUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADSW2MA0ltjN; channel=baidusearch; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BK_SEARCHLOG=%7B%22key%22%3A%5B%22%E8%B1%A1%E5%B1%B1%E7%BA%A2%E7%BE%8E%E4%BA%BA%22%2C%22ufotable%22%2C%22konami%22%2C%22mmorpg%22%2C%22%E5%BC%82%E4%B8%96%E7%95%8C%E8%88%85%E8%88%85%22%2C%22%E6%97%A0%E5%A4%B4%E9%AA%91%E5%A3%AB%E5%BC%82%E9%97%BB%E5%BD%95%22%2C%22%E6%B3%A2%E8%A5%BF%E4%BA%9A%E6%97%B6%E5%85%89%22%2C%22%E5%B4%A9%E5%9D%8F%E5%AD%A6%E5%9B%AD2%22%2C%22%E5%B4%A9%E5%9D%8F%E5%AD%A6%E5%9B%AD%22%2C%22%E5%92%92%E6%9C%AF%E5%9B%9E%E6%88%982%22%5D%7D; BAIDUID_BFESS=D6816D0EBB7EFCA18C0ABF5FBED0D1A7:SL=0:NR=10:FG=1; PSINO=5; delPer=0; BA_HECTOR=0ga0052l202l2g00ah84057b1hrb2tp1j; ZFY=HjkH2YLBDC36WVlp49oM7KlY8A18EgcH3VE8dT1kuDU:C; H_PS_PSSID=36544_37972_38011_37623_36920_37990_37793_37930_37900_26350_37959_37881; zhishiTopicRequestTime=1672896299137; baikeVisitId=819729ef-ccdf-4639-94a2-320fe8143999; X_ST_FLOW=0; Hm_lvt_55b574651fcae74b0a9f1cf9c8d7c93a=1672714412,1672731522,1672735774,1672896304; __bid_n=185723fa0ee687413c4207; FEID=v10-ff647883545fc9e9ca5d9748e14d9cf676637e3e; __xaf_fpstarttimer__=1672896304162; __xaf_ths__={"data":{"0":1,"1":43200,"2":60},"id":"1820e30c-af5a-49e9-a7c1-e9860977bc11"}; __xaf_thstime__=1672896304315; FPTOKEN=1mhc7NCbicRsF/f0kywFFe317NlJZvJhUqv9k5tgVB0jIa+XWakghPlM2+O+KyQap8Rqlx8oGSY34xohwVGZrenPWB1dY+xI6jz4S5UTljTfHYcyYAarLpgOPgIC5qgs/OagSLrtf8QNt5FO1LTOGWi3k4oD2r8HSdI0KKCFh4Cpv92mjgIlSf/gtPaU4nhV64p5/ANQBjI5y6zxDYc5bmX9cUpjLJBHmQyH7SC4Fkg67T9EfbVFcbBwHaI2gMEoUfTiz/YGZPXCi4PEce0JFXmnuXt2AGUgsYC/eBsludolQcRkyWrLHwo1Ypkuk7JMTyRp5gUoun7W/8PkT9fsVGNRqxbTlp7nI9g1vw+iHTN80oxEFeF6zbuO3ACTAaXbfL/nG2AcBufawzvouJ5cQg==|y2kWEaqMe3RkdlXAgdwA36SGp22boesKddHiiz7mG1s=|10|24fcf5c0247b5923d495599f4db1183d; __xaf_fptokentimer__=1672896304439; RT="z=1&dm=baidu.com&si=vfirebd5q6k&ss=lcin9h1p&sl=j&tt=co1&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf"; Hm_lpvt_55b574651fcae74b0a9f1cf9c8d7c93a=1672896405; ab_sr=1.0.1_MTFjYzVkNjIwMDY0MjE1ODI0Y2I5MGE5Y2E5MzAxNGY2NGI5NTQzNDgyMTgwMDA4NzhkMzBlN2U0MDA2OGUyYWVlNjQ4ZmU4NDU4ODU1NDcwNmI5NTljMzU0NjcwMTM0YWYzZGZiNWRkNjA5NzNhY2I4OWQ3NzdmNTAzNWZlOTkwNjZjOTE2YjMxYzU2NmQ2ODRmMjQxZmJjMjUwY2E3NDdhNDIwYzg4MDA5YjA3N2Q3MzllOTA4ZmViZjdiOGFj`)
	req.Header.Set("Referer", "https://baike.baidu.com/item/%E6%9D%8E%E7%99%BD/1043?fromModule=lemma_search-box")
	req.Header.Set("Sec-Fetch-Dest", "empty")
	req.Header.Set("Sec-Fetch-Mode", "cors")
	req.Header.Set("Sec-Fetch-Site", "same-origin")
	req.Header.Set("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")
	req.Header.Set("X-Requested-With", "XMLHttpRequest")
	req.Header.Set("sec-ch-ua", `"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"`)
	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)

	var res Demo
	// 将响应反序列化成结构体
	err2 := json.Unmarshal(bodyText, &res)
	if err2 != nil {
		log.Fatal(err2)
	}

	fmt.Println(res.List)

}