go爬虫

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过期没调好

上一篇:golang gin框架 路由重定向(get, post,handler传参数)


下一篇:[转]mysql-5.6.17-win32免安装版配置