Skip to content

基本用法


go
package main

import (
	"fmt"
	"github.com/gocolly/colly"
	"github.com/gocolly/colly/queue"
)

func main() {
	url := "https://httpbin.org/delay/1"

	// 默认 collector
	c := colly.NewCollector()

	// 创建一个2个消费线程的请求队列
	q, _ := queue.New(
		2, // 2个消费线程
		&queue.InMemoryQueueStorage{MaxSize: 10000}, // 使用默认队列存储
	)

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("visiting", r.URL)
	})

	for i := 0; i < 5; i++ {
		// 添加URL到队列
		q.AddURL(fmt.Sprintf("%s?n=%d", url, i))
	}
	// 消费队列 URL
	q.Run(c)

}