Golang 网络爬虫框架gocolly/colly 一

Golang 网络爬虫框架gocolly/colly

gocolly是用go实现的网络爬虫框架,目前在github上具有3400+星,名列go版爬虫程序榜首。gocolly快速优雅,在单核上每秒可以发起1K以上请求;以回调函数的形式提供了一组接口,可以实现任意类型的爬虫;依赖goquery库可以像jquery一样选择web元素。

gocolly的官方网站是http://go-colly.org/,提供了详细的文档和示例代码。安装colly:

 

go get -u github.com/gocolly/colly/...

  

 

在代码中导入包:

import "github.com/gocolly/colly"

  

colly的主体是Collector对象,管理网络通信和负责在作业运行时执行附加的回掉函数。使用colly需要先初始化Collector

c := colly.NewCollector()

   

可以向colly附加各种不同类型的回掉函数,来控制收集作业或获取信息。增加回掉函数:

 

c.OnRequest(func(r *colly.Request) {

    fmt.Println("Visiting", r.URL)

})

c.OnError(func(_ *colly.Response, err error) {

    log.Println("Something went wrong:", err)

})

c.OnResponse(func(r *colly.Response) {

    fmt.Println("Visited", r.URL)

})

c.OnHTML("a[href]", func(e *colly.HTMLElement) {

    e.Request.Visit(e.Attr("href"))

})

c.OnHTML("tr td:nth-of-type(1)", func(e *colly.HTMLElement) {

    fmt.Println("First column of a table row:", e.Text)

})

c.OnScraped(func(r *colly.Response) {

    fmt.Println("Finished", r.URL)

})

  

 

回掉函数的调用顺序如下:

1. OnRequest

在发起请求前被调用

2. OnError

请求过程中如果发生错误被调用

3. OnResponse

收到回复后被调用

4. OnHTML

OnResponse之后被调用,如果收到的内容是HTML

5. OnScraped

OnHTML之后被调用

 

官方提供的Basic示例代码:

 

package main

 

import (

    "fmt"

 

    "github.com/gocolly/colly"

)

 

func main() {

    // Instantiate default collector

    c := colly.NewCollector()

 

    // Visit only domains: hackerspaces.org, wiki.hackerspaces.org

    c.AllowedDomains = []string{"hackerspaces.org", "wiki.hackerspaces.org"}

 

    // On every a element which has href attribute call callback

    c.OnHTML("a[href]", func(e *colly.HTMLElement) {

        link := e.Attr("href")

        // Print link

        fmt.Printf("Link found: %q -> %s\n", e.Text, link)

        // Visit link found on page

        // Only those links are visited which are in AllowedDomains

        c.Visit(e.Request.AbsoluteURL(link))

    })

 

    // Before making a request print "Visiting ..."

    c.OnRequest(func(r *colly.Request) {

        fmt.Println("Visiting", r.URL.String())

    })

 

    // Start scraping on https://hackerspaces.org

    c.Visit("https://hackerspaces.org/")

}

  

 

 

该实例程序仅访问hackerspaces.org域内的链接,OnHTML回掉函数的选择器为a[href],选择页面内具有href属性的a类型元素,找到链接后继续抓取。 运行的部分结果如下:

PS E:\mygo\src\github.com\gocolly\colly\_examples\basic> .\basic.exe

Visiting https://hackerspaces.org/

Link found: "navigation" -> #column-one

Link found: "search" -> #searchInput

Link found: "" -> /File:Cbase07.jpg

Visiting https://hackerspaces.org/File:Cbase07.jpg

Link found: "navigation" -> #column-one

Link found: "search" -> #searchInput

Link found: "File" -> #file

Link found: "File history" -> #filehistory

Link found: "File usage" -> #filelinks

Link found: "" -> /images/e/ec/Cbase07.jpg

Visiting https://hackerspaces.org/images/e/ec/Cbase07.jpg

Link found: "800 × 600 pixels" -> /images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpg

Visiting https://hackerspaces.org/images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpg

 

posted @ 2017-12-29 19:57  majianguo  阅读(16816)  评论(0编辑  收藏  举报