go爬虫

2021/8/15 23:06:37

本文主要是介绍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过期没调好



这篇关于go爬虫的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程