go 爬取页面保存

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)
func main() {

	client := &http.Client{}

	req,err := http.NewRequest("GET","http://tool.yeves.cn/",nil)
	if err != nil{
		fmt.Println(err)
	}

	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.2222")

	resp,err := client.Do(req)
	defer resp.Body.Close()

	if err != nil{
		fmt.Println("err")
	}

	body,err := ioutil.ReadAll(resp.Body)
	if err != nil{
		fmt.Println(err)
	}
	writeToFile(string(body))
}

func writeToFile(str string )  {
	filePath := "./a.html"
	file, err := os.OpenFile(filePath, os.O_WRONLY | os.O_CREATE, 0666)
	if err != nil {
		fmt.Printf("open file err=%v\n", err)
		return
	}
	//及时关闭file句柄
	defer file.Close()
	//写入时,使用带缓存的 *Writer
	writer := bufio.NewWriter(file)
	for i := 0; i < 5; i++ {
		writer.WriteString(str)
	}

}

  

posted @ 2020-06-11 15:47  brady-wang  阅读(432)  评论(0编辑  收藏  举报