fetch

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fetch reads one public web page per call: public addresses only, no crawling, no cookies. By default a client with a Chrome TLS fingerprint goes first, with a plain Go client as backup for its library's own TLS or protocol errors; robots.txt is consulted only when asked.

Index

Constants

View Source
const (
	ClientChromeLike  = "chrome_like"
	ClientPlain       = "plain"        // the plain client as the first step (Impersonate off or fallback)
	ClientPlainBackup = "plain_backup" // the plain client after the Chrome-like client errored
)

Clients, the steps of the ladder that produced an attempt.

View Source
const (
	OutcomeOK         = "ok"
	OutcomeRefused    = "refused"    // HTTP 403 or 429
	OutcomeChallenge  = "challenge"  // HTTP 503 or a bot-challenge page
	OutcomeNeedsJS    = "needs_js"   // an HTML shell whose content only JavaScript would render
	OutcomeTimeout    = "timeout"    // the client's time budget ran out
	OutcomeError      = "error"      // network error, other HTTP status, address refusal
	OutcomeDisallowed = "disallowed" // robots.txt, only with RespectRobots
)

Outcomes of one attempt.

View Source
const (
	ImpersonateAlways   = "always"   // Chrome-like first, plain backup on its TLS or protocol errors (default)
	ImpersonateFallback = "fallback" // plain first, Chrome-like when the plain client is refused or challenged
	ImpersonateOff      = "off"      // plain client only
)

Impersonation modes.

View Source
const (
	DefaultUserAgent   = "refetch"
	DefaultRobotsAgent = "refetch"
	DefaultTimeout     = 20 * time.Second
	MaxBytes           = 2 << 20
)

Defaults for the zero Fetcher.

Variables

This section is empty.

Functions

func Blocked

func Blocked(status int, body []byte) bool

Blocked reports an anti-bot refusal: HTTP 403 or 429, or a challenge.

func Challenge

func Challenge(status int, body []byte) bool

Challenge reports whether a response is a bot challenge rather than the page: HTTP 503, HTTP 202 (AWS WAF answers a GET with an empty 202 and a challenge script), or a short page carrying a challenge marker.

func Clip

func Clip(s string, n int) string

Clip cuts s to n runes, marking the cut.

func DialContext

func DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext dials the way both clients do: public addresses only, checked after DNS resolution. The headless Chrome step's proxy dials with it.

func EmptyRendered

func EmptyRendered(dom []byte) bool

EmptyRendered reports whether a DOM from headless Chrome has next to no text. Unlike NeedsJS it ignores scripts and mount points, which stay.

func NeedsJS

func NeedsJS(body []byte, contentType string) bool

NeedsJS reports whether an HTML page is a JavaScript-only shell: under about 200 characters of readable text plus an empty mount point or an enable-JavaScript notice, or under 100 characters plus a script (an app shell, or a proof-of-work page that solves a puzzle and resubmits).

func Text

func Text(body []byte, contentType string) string

Text returns readable text: HTML without scripts, styles and tags; other types as is.

Types

type Attempt

type Attempt struct {
	URL, Host, Client, Outcome string
	Status                     int
	Bytes                      int
	Duration                   time.Duration
	Err                        string
}

Attempt is one client's try at a page, reported to Fetcher.Record.

type Fetcher

type Fetcher struct {
	Impersonate   string        // ImpersonateAlways when empty
	RespectRobots bool          // consult robots.txt before fetching
	UserAgent     string        // the plain client's User-Agent; DefaultUserAgent when empty
	RobotsAgent   string        // the robots.txt agent name; DefaultRobotsAgent when empty
	Timeout       time.Duration // per client; DefaultTimeout when zero
	Record        func(Attempt) // called after every attempt, if set
	// contains filtered or unexported fields
}

Fetcher gets pages. The zero value impersonates Chrome first and ignores robots.txt.

func (*Fetcher) Get

func (f *Fetcher) Get(ctx context.Context, rawURL string) (Page, error)

Get fetches rawURL, walking the clients the impersonation mode allows. The error names why the page could not be read.

type Page

type Page struct {
	Body        []byte
	ContentType string
	Last        Attempt   // the attempt that produced Body, or the last failure
	Attempts    []Attempt // every attempt of this call, in order
}

Page is a fetched page, or the attempts that failed to fetch it.

Jump to

Keyboard shortcuts

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