scavenge

package module
v0.0.0-...-000c55a Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 15 Imported by: 0

README

scavenge

A modern, batteries-included, modular scraping framework for Go.

Go Reference

Installation

go get github.com/LQR471814/scavenge

Features

  • Minimal footprint: ~1k LOC with minimal reliance on third-party libraries.
  • Batteries-included: Domain whitelisting, automatic throttling, retries, replaying responses, etc... all included.
  • Flexible: Near-full control over the details of scraping, but no need to concern yourself with the details if you don't need to.
  • Extensible: Easily add features you need with simple middleware interfaces.

Example

Here's an example that scrapes ~1000 wikipedia pages in ~20 seconds.

To run it:

cd examples/wikipedia
go run .

Dependencies

  • golang.org/x/net - Used in downloader.Response.
  • github.com/PuerkitoBio/purell - Used only in middleware.Dedupe and middleware.Replay.
  • github.com/gobwas/glob - Used only in middleware.AllowedDomains.
  • github.com/zeebo/xxh3 - Used only in middleware.FSReplayStore.
  • All the other dependencies are only used in examples.

Credits

  • Python's scrapy is a massive influence on this library, many of the design choices in this library are taken straight from here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShortUrl

func ShortUrl(u *url.URL) string

ShortUrl formats a url.URL without its schema for use in logging and errors.

func WithOnItemProcessorFail

func WithOnItemProcessorFail(callback func(i items.Item, err error)) option

WithOnItemProcessorFail sets the error handler for item processing errors.

func WithOnRequestFail

func WithOnRequestFail(fn func(req *downloader.Request, err error)) option

WithOnRequestFail sets the error handler for requests that:

  • fail before sending, either due to a malformed request or failed middleware.
  • fail while sending, due to some http transport error.
  • fail after receiving a response that causes an error in the downloader's middleware.

func WithOnSpiderHandlerFail

func WithOnSpiderHandlerFail(callback func(res *downloader.Response, err error)) option

WithOnSpiderHandlerFail sets the error handler for errors that occur within Spider.HandleResponse

func WithParallelDownloads

func WithParallelDownloads(count int) option

WithParallelDownloads sets the amount of requests and responses that can be processed in parallel.

func WithParallelItems

func WithParallelItems(count int) option

WithParallelItems sets the amount of items that can be processed in parallel.

func WithRetryDelayBounds

func WithRetryDelayBounds(minDelay, maxDelay time.Duration) option

WithRetryDelayBounds sets the bounds for retry delay.

Types

type Logger

type Logger interface {
	Debug(component, msg string, args ...any)
	Info(component, msg string, args ...any)
	Warn(component, msg string, args ...any)
	Error(component, msg string, args ...any)
}

Logger is an interface containing logging reporters.

Note: the args of any logging methods (Debug, Info, Warn, Error) are a list of keys and values in alternating order like it is in log/slog(https://pkg.go.dev/log/slog).

func LoggerFromContext

func LoggerFromContext(ctx context.Context) Logger

LoggerFromContext retrieves a Logger from the given context, it will panic if the Logger is not there.

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

Navigator contains methods for saving items and following urls typically used in a spider's response handler.

func (n Navigator) AnchorRequest(a *html.Node) (*downloader.Request, error)

AnchorRequest returns a GET request to the given url created by resolving the href of the given a-tag with the current url as the base url.

func (n Navigator) AnchorUrl(a *html.Node) (*url.URL, error)

AnchorUrl returns the absolute url referenced by an anchor tag (created by resolving the href with the current url as the base url).

func (n Navigator) Context() context.Context

Context returns the scraping context.

func (n Navigator) CurrentUrl() *url.URL

CurrentUrl returns the current url of the navigator.

func (n Navigator) Request(req *downloader.Request)

Request queues the given request.

func (n Navigator) SaveItem(value any)

SaveItem queues the given item for processing.

type Perf

type Perf interface {
	Elapsed(component, operation string, milliseconds uint64)
}

Perf is an interface containing performance reporters.

type Scavenger

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

Scavenger is the main component that schedules requests and processes items concurrently, it handles retry logic and pausing/resuming scraping.

func NewScavenger

func NewScavenger(
	dl downloader.Downloader,
	iproc items.Processor,
	logger Logger,
	options ...option,
) *Scavenger

func ScavengerFromContext

func ScavengerFromContext(ctx context.Context) *Scavenger

ScavengerFromContext returns Scavenger from a given context.

This will only return a scavenger if called within a spider's HandleResponse, item pipeline, or middleware.

func (*Scavenger) QueueItem

func (s *Scavenger) QueueItem(i items.Item)

func (*Scavenger) QueueRequest

func (s *Scavenger) QueueRequest(req *downloader.Request, referer *url.URL)

func (*Scavenger) Run

func (s *Scavenger) Run(ctx context.Context, spider Spider)

Run runs the given spider on the scavenger.

Note:

  • Run is not concurrency-safe, it should only be executed one-at-a-time for a given Scavenger.

type SlogLogger

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

SlogLogger implements Logger using a log/slog(https://pkg.go.dev/log/slog) logger.

func NewSlogLogger

func NewSlogLogger(log *slog.Logger, enablePerf bool) SlogLogger

NewSlogLogger creates a new SlogLogger using a given slog.Logger

If enablePerf is false, all calls to Elapsed will be a no-op, otherwise, the elapsed statistics will be periodically printed to the console as DEBUG-level messages.

func (SlogLogger) Debug

func (t SlogLogger) Debug(component, msg string, args ...any)

func (SlogLogger) Error

func (t SlogLogger) Error(component, msg string, args ...any)

func (SlogLogger) Info

func (t SlogLogger) Info(component, msg string, args ...any)

func (SlogLogger) Warn

func (t SlogLogger) Warn(component, msg string, args ...any)

type Spider

type Spider interface {
	StartingRequests() []*downloader.Request
	HandleResponse(nav Navigator, res *downloader.Response) error
}

Spider contains the business logic of navigating to different links and storing structured data from the unstructured page.

Note: HandleResponse should be safe to be called concurrently, if it is too hard to make it so, make sure that any scavenger that uses this spider has the option WithDownloadWorkerCount set to 1.

Directories

Path Synopsis
examples
netsuite-docs command
wikipedia command

Jump to

Keyboard shortcuts

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