gugo

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

gugo

gugo is a go version of the asynchronous crawler framework

快速开始

package main

import (
	"fmt"
	"github.com/xiaogogonuo/gugo"
	"net/http"
)

// Film 客户端自定义模型
type Film struct {
	score uint
	title string
}

type MySpider struct {
	*gugo.GuGo
	block chan struct{} // 客户端数据处理阻塞控制器,防止爬虫结束,数据处理被迫停止
}

func (ms *MySpider) Parse1(response *gugo.Response) {
	// 获取响应数据的字节流形式
	_ = response.Body()
	// 获取响应数据的字符串形式
	_ = response.Text()
	// 获取请求携带过来的元数据
	_ = response.Meta()
	// 获取响应所对应的请求链接
	_ = response.URL()
	// 获取响应所对应的请求方法
	_ = response.Method()

	// 模拟发送从页面提取的数据
	ms.Push(Film{score: 5, title: "肖申克的救赎"})

	// 模拟发送从页面提取的新链接
	// Request方法是简易版的GET请求,客户端只需传入URL,自定义解析函数,元数据即可
	// 新链接:必填
	// 解析器:必填
	// 元数据:可选
	ms.Request("https://www.douban.com", ms.Parse2, map[string]interface{}{"parser": "Parser1"})
}

func (ms *MySpider) Parse2(response *gugo.Response) {
	// 获取从Parser1传入的元数据
	_ = response.Meta()

	// 模拟发送从页面提取的数据
	ms.Push(Film{score: 4, title: "越狱"})

	// 模拟发送从页面提取的新链接
	// NativeRequest方法是原生版的http请求,客户端需要传入自定义*http.Request、自定义解析函数、元数据
	// 新请求:必填
	// 解析器:必填
	// 元数据:可选
	customGetRequest, _ := http.NewRequest(http.MethodGet, "https://www.tencent.com", nil)
	ms.NativeRequest(customGetRequest, ms.Parser3, map[string]interface{}{"parser": "Parser2"})

	customPostRequest, _ := http.NewRequest(http.MethodPost, "https://www.abc.com", nil)
	ms.NativeRequest(customPostRequest, ms.Parser3, map[string]interface{}{"parser": "Parser2"})
}

func (ms *MySpider) Parser3(response *gugo.Response) {
	// 获取从Parser2传入的元数据
	_ = response.Meta()

	// 模拟发送从页面提取的数据
	ms.Push(Film{score: 3, title: "复仇者联盟"})
}

// ProcessItem 客户端自定义数据处理器
func (ms *MySpider) ProcessItem() {
	for item := range ms.Pull() {
		fmt.Println(item)
	}
	ms.block <- struct{}{}
}

func main() {
	// 1、创建我的爬虫
	ms := &MySpider{
		GuGo:  gugo.CreateGuGo(),
		block: make(chan struct{}),
	}
	// 2、发送初始请求
	ms.Request("https://www.baidu.com", ms.Parse1, nil)
	// 3、异步处理客户端数据(保存到文件、数据库等操作)
	go ms.ProcessItem()
	// 4、启动并发爬虫系统
	ms.GooGol()
	// 5、客户端数据处理阻塞
	<-ms.block
}

关于作者

xiaogogonuo@163.com

Documentation

Index

Constants

View Source
const (
	MaxRetry         = 5                // 默认最大下载重试次数
	ConnectTimeout   = 10 * time.Second // 默认客户端连接超时时间
	ReadWriteTimeout = 10 * time.Second // 默认客户端读写超时时间
)
View Source
const (
	MaxIdle   = 10          // 默认最大休眠次数
	HeartBeat = time.Second // 默认心跳检测间隔时间
)
View Source
const (
	PipelineBufCap     = 1 << 12 // 默认数据队列容量
	ConcurrentPipeline = 1 << 10 // 默认数据处理的并发量
)
View Source
const (
	FalsePositive     = 0.01    // 默认过滤错误容忍率
	EstimateRequest   = 1000000 // 默认估计100万请求
	RequestBufferCap  = 1 << 12 // 默认请求队列容量
	ConcurrentRequest = 1 << 10 // 默认并发请求数
)
View Source
const (
	ResponseBufCap     = 1 << 12 // 默认响应队列容量
	ConcurrentResponse = 1 << 10 // 默认响应处理的并发量
)

Variables

View Source
var (
	// RetryHTTPCode 默认请求失败时允许重新下载的状态码
	RetryHTTPCode = []int{500, 502, 503, 504, 408}
)

Functions

This section is empty.

Types

type GuGo

type GuGo struct {
	// contains filtered or unexported fields
}

func CreateGuGo

func CreateGuGo() *GuGo

CreateGuGo 😄创建谷歌😄

func (*GuGo) GooGol

func (g *GuGo) GooGol()

GooGol 谷歌运行入口

func (*GuGo) NativeRequest

func (g *GuGo) NativeRequest(r *http.Request, parser Parser, meta map[string]interface{})

NativeRequest 原生请求,客户端自定义

func (*GuGo) Pull

func (g *GuGo) Pull() chan interface{}

Pull 客户端下载数据

func (*GuGo) Push

func (g *GuGo) Push(item interface{})

Push 客户端发送数据

func (*GuGo) Request

func (g *GuGo) Request(url string, parser Parser, meta map[string]interface{})

Request 简易版GET请求

func (GuGo) SetHearBeat

func (e GuGo) SetHearBeat(heartbeat time.Duration)

SetHearBeat 设置心跳检测间隔时间

func (GuGo) SetMaxIdle

func (e GuGo) SetMaxIdle(n uint64)

SetMaxIdle 设置最大休眠次数

type Parser

type Parser func(*Response)

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func (*Response) Body

func (r *Response) Body() []byte

func (Response) FingerPrint

func (r Response) FingerPrint() []byte

FingerPrint 请求指纹:sha1(请求体+请求URL+请求方法)

func (Response) FingerPrintS

func (r Response) FingerPrintS() string

FingerPrintS 请求指纹字符串

func (Response) Host

func (r Response) Host() string

func (*Response) Meta

func (r *Response) Meta() map[string]interface{}

func (Response) Method

func (r Response) Method() string

func (Response) Schema

func (r Response) Schema() string

func (*Response) Text

func (r *Response) Text() string

func (Response) URL

func (r Response) URL() string

func (*Response) Valid

func (r *Response) Valid() bool

func (*Response) XPath

func (r *Response) XPath(selector string) *goquery.Selection

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL