内容简介:本文介绍网页抓取相关的知识我们使用可以看到,该方法返回了一个
本文介绍网页抓取相关的知识
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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Python爬虫:抓取新浪新闻数据
- Python爬虫:抓取新浪新闻数据
- python爬虫-- 抓取网页、图片、文章
- Python爬虫抓取技术的门道
- 如何使用代理IP进行数据抓取,PHP爬虫抓取亚马逊商品数据
- 利用Python网络爬虫抓取网易云歌词
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python for Data Analysis
Wes McKinney / O'Reilly Media / 2012-11-1 / USD 39.99
Finding great data analysts is difficult. Despite the explosive growth of data in industries ranging from manufacturing and retail to high technology, finance, and healthcare, learning and accessing d......一起来看看 《Python for Data Analysis》 这本书的介绍吧!