colly 使用多个收集器 ##8

使用多个收集器

如果任务足够复杂或具有不同类型的子任务,建议为一个抓取工作使用多个收集器。coursera course scraper就是一个很好的例子,它使用了两个收集器——一个解析列表视图并处理分页,另一个收集课程的详细信息。

注意:使用收集器。ID在调试中区分不同的收集器

克隆收集器

如果收集器具有类似的配置,可以使用收集器的Clone()方法。Clone()复制具有相同配置但没有附加回调的收集器。

c := colly.NewCollector(
	colly.UserAgent("myUserAgent"),
	colly.AllowedDomains("foo.com", "bar.com"),
)
// Custom User-Agent and allowed domains are cloned to c2
c2 := c.Clone()

  

在收集器之间传递自定义数据

使用collector的Request()函数可以与其他收集器共享上下文。

共享上下文的例子:

c.OnResponse(func(r *colly.Response) {
	r.Ctx.Put(r.Headers.Get("Custom-Header"))
	c2.Request("GET", "https://foo.com/", nil, r.Ctx, nil)
})

  

原文地址:https://www.cnblogs.com/liujie-php/p/11571117.html