手摸手教你用go写爬虫之一(准备知识:网页抓取)

栏目: Go · 发布时间: 6年前

内容简介:本文介绍网页抓取相关的知识我们使用可以看到,该方法返回了一个

本文介绍网页抓取相关的知识

1. 获取网页内容

我们使用 http.Get() 方法来获取网页的内容,它相当于 PHP 中的 file_get_contents

url := "https://hz.zu.anjuke.com/"
response,err := http.Get(url)

可以看到,该方法返回了一个 response 相应信息的指针以及错误信息 该响应信息中我们要获取的是请求体的内容,可以使用:

bytes, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()

注意: response.Body 必须手动调用 Close 方法,否则该网络响应会一直占用内存 原官方文档如下:

// The http Client and Transport guarantee that Body is always
 // non-nil, even on responses without a body or responses with
 // a zero-length body. It is the caller's responsibility to
 // close Body.

这里我们就拿到了完整的字节流请求的结果。

2. 完整实例

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

/**
  根据提供的url 获取返回信息内容
 */
func GetContents(url string) (string ,error) {

	resp,err := http.Get(url)
	if err != nil {
		return "",err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("get content failed status code is %d ",resp.StatusCode)
	}

	bytes,err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "" , nil
	}
	return string(bytes),nil
}

func main() {
	url := "https://hz.zu.anjuke.com/"
	contents,err := GetContents(url)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf(contents)
}

源代码地址: github


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First jQuery

Head First jQuery

Ryan Benedetti , Ronan Cranley / O'Reilly Media / 2011-9 / USD 39.99

Want to add more interactivity and polish to your websites? Discover how jQuery can help you build complex scripting functionality in just a few lines of code. With Head First jQuery, you'll quickly g......一起来看看 《Head First jQuery》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具