package main import ( "fmt" "io/ioutil" "net/http" "github.com/gin-gonic/gin" ) func get_baidu(url string) string{ client := &http.Client{} req, _ := http.NewRequest("GET", url, nil) req.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36") res, err := client.Do(req) if err != nil{ fmt.Println("get error") return "" } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil{ fmt.Println("body error") return "" } return string(body) } func main() { r := gin.Default() name := "asf" html := get_baidu("https://www.baidu.com/s?wd="+name) fmt.Printf(html) }
爬一下百度,然后本来想用gin返回的,可惜ide过期没调好