colly 使用多個收集器 ##8

使用多個收集器

若是任務足夠複雜或具備不一樣類型的子任務,建議爲一個抓取工做使用多個收集器。coursera course scraper就是一個很好的例子,它使用了兩個收集器——一個解析列表視圖並處理分頁,另外一個收集課程的詳細信息。dom

注意:使用收集器。ID在調試中區分不一樣的收集器函數

 

克隆收集器

若是收集器具備相似的配置,能夠使用收集器的Clone()方法。Clone()複製具備相同配置但沒有附加回調的收集器。spa

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()函數能夠與其餘收集器共享上下文。調試

共享上下文的例子:blog

c.OnResponse(func(r *colly.Response) {
	r.Ctx.Put(r.Headers.Get("Custom-Header"))
	c2.Request("GET", "https://foo.com/", nil, r.Ctx, nil)
})
相關文章
相關標籤/搜索