项目地址:https://github.com/zhshch2002/goribot
使用文档:https://imagician.net/goribot/
Feature
- 优雅的 API
- 整洁的文档
- 高速(单核处理 >1K task/sec)
- 友善的分布式支持
- 丰富的扩展支持
- 请求去重(????支持分布式)
- 限制请求、速率、并发
- Json,CSV 存储结果
- Robots.txt 支持
- 记录请求异常
- 随机 UA 、随机代理
- 失败重试
- 轻量,适于学习或快速开箱搭建
Demo
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/zhshch2002/goribot"
)
func main() {
s := goribot.NewSpider() // 创建了一个爬虫
var h goribot.CtxHandlerFun // 这是一个回调函数,用于发现新链接(不把他用 var 单出来声明就没法在回调函数内调用自己)
h = func(ctx *goribot.Context) {
fmt.Println(ctx.Resp.Request.URL)
if ctx.Resp.Dom != nil {
ctx.Resp.Dom.Find("a").Each(func(i int, sel *goquery.Selection) {
if u := sel.AttrOr("href", ""); u !="" {
// ???? 注意在这里不是 s.AddTask 而是 ctx.AddTask
ctx.AddTask(goribot.GetReq(u), h)
}
})
}
}
// 使用回调函数 h 来创建种子任务
s.AddTask(goribot.GetReq("https://httpbin.org"), h)
s.Run()
}
分布式爬虫
有疑问加站长微信联系(非本文作者)
