Golang核心编程(8)-net/http包的使用

版权声明:需要引用、发表的朋友请与本人联系 https://blog.csdn.net/pbrlovejava/article/details/84112456


更多关于Golang核心编程知识的文章请看:Golang核心编程(0)-目录页


golang可以快速方便地实现简单的爬虫,本次我将使用net/http包、goquery库,具体的使用大家可以查询文档说明,本文只做一个简单的介绍。最后以一个爬取CSDN的推荐文章列表的爬虫来演示golang爬虫的使用。

一、net/http包

net/http包是golang的内置标准库之一,在爬虫的应用中,它主要用于伪造请求去获得响应,从而将其交给其他程序用以解析,主要的方法有Get()、Post()、PostFrom()和Do()。

1.1、Get请求

get请求可以直接http.Get方法

func main() {
resp, err := http.Get("http://www.baidu.com")
	if err != nil {
		// handle error
		log.Println(err)
		return
	}
	//延迟关闭resp.Body,它是一个ioReader
	defer resp.Body.Close()
	//获得响应头
	headers := resp.Header

	for k, v := range headers {
		fmt.Printf("k=%v, v=%v\n", k, v)
	}
	//响应状态
	fmt.Printf("resp status %s,statusCode %d\n", resp.Status, resp.StatusCode)
	//协议
	fmt.Printf("resp Proto %s\n", resp.Proto)
	//响应体长度
	fmt.Printf("resp content length %d\n", resp.ContentLength)
	//编码表
	fmt.Printf("resp transfer encoding %v\n", resp.TransferEncoding)

	fmt.Printf("resp Uncompressed %t\n", resp.Uncompressed)
	//响应体,是主要解析的内容
	fmt.Println(reflect.TypeOf(resp.Body)) // *http.gzipReader

	buf := bytes.NewBuffer(make([]byte, 0, 512))

	length, _ := buf.ReadFrom(resp.Body)

	fmt.Println(len(buf.Bytes()))
	fmt.Println(length)
	fmt.Println(string(buf.Bytes()))
}
}

1.2、Do方法

有时需要在请求的时候设置头参数、cookie之类的数据,就可以使用http.Do方法。

func main() {
	client := &http.Client{}

	req, err := http.NewRequest("POST", "http://www.maimaiche.com/loginRegister/login.do",
		strings.NewReader("mobile=xxxxxxxxx&isRemberPwd=1"))
	if err != nil {
		log.Println(err)
		return
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return
	}

	fmt.Println(resp.Header.Get("Content-Type")) //application/json;charset=UTF-8

	type Result struct {
		Msg    string
		Status string
		Obj    string
	}

	result := &Result{}
	json.Unmarshal(body, result) //解析json字符串

	if result.Status == "1" {
		fmt.Println(result.Msg)
	} else {
		fmt.Println("login error")
	}
	fmt.Println(result)
}

1.3、Post请求

如果使用http POST方法可以直接使用http.Post 或 http.PostForm


func main() {
	resp, err := http.Post("http://www.maimaiche.com/loginRegister/login.do",
		"application/x-www-form-urlencoded",
		strings.NewReader("mobile=xxxxxxxxxx&isRemberPwd=1"))
	if err != nil {
		fmt.Println(err)
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(string(body))
}

1.4、PostForm方法

用以提交表单并获得响应

func main() {

	postParam := url.Values{
		"mobile":      {"xxxxxx"},
		"isRemberPwd": {"1"},
	}

	resp, err := http.PostForm("http://www.maimaiche.com/loginRegister/login.do", postParam)
	if err != nil {
		fmt.Println(err)
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(string(body))
}

猜你喜欢

转载自blog.csdn.net/pbrlovejava/article/details/84112456
今日推荐