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
- Variables
- func Blocked(status int, body []byte) bool
- func Challenge(status int, body []byte) bool
- func Clip(s string, n int) string
- func DialContext(ctx context.Context, network, address string) (net.Conn, error)
- func EmptyRendered(dom []byte) bool
- func Markdown(body []byte, contentType string, page *url.URL) string
- func NeedsJS(body []byte, contentType string) bool
- func NotText(contentType string, body []byte) string
- func Text(body []byte, contentType string) string
- func UseProxy() string
- type Attempt
- type Fetcher
- type Page
Constants ¶
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.
const ( OutcomeOK = "ok" OutcomeRefused = "refused" // HTTP 403 or 429 OutcomeChallenge = "challenge" // HTTP 503 or a bot-challenge page, even on a 403 (Cloudflare's is) 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.
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.
const ( DefaultUserAgent = "refetch" DefaultRobotsAgent = "refetch" DefaultTimeout = 20 * time.Second MaxBytes = 2 << 20 )
Defaults for the zero Fetcher.
Variables ¶
var ErrNotText = errors.New("not a web page or text")
ErrNotText is a page that is a file, such as a PDF or an image, rather than HTML or text.
Functions ¶
func Challenge ¶
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 DialContext ¶
DialContext dials the way both clients do: public addresses only, checked after DNS resolution, or through the proxy UseProxy set. The headless Chrome step's proxy dials with it.
func EmptyRendered ¶
EmptyRendered reports whether a DOM from headless Chrome has next to no text. Unlike NeedsJS it ignores scripts and mount points, which stay.
func Markdown ¶ added in v0.2.0
Markdown returns an HTML page as Markdown: headings, lists, links, tables and code blocks kept; scripts, styles and hidden elements dropped. Links resolve against page (a <base href> wins); only http, https and mailto links keep their target. Other types come back as Text returns them.
func NeedsJS ¶
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 NotText ¶ added in v0.2.0
NotText is the media type of a body that is not text, or "" for text. The body decides when the server named no type, or named a text type over PDF bytes.
func UseProxy ¶ added in v0.2.0
func UseProxy() string
UseProxy sends every connection through the proxy in HTTPS_PROXY (or HTTP_PROXY) as a CONNECT tunnel, except to hosts NO_PROXY lists. TLS runs inside the tunnel, so the site still sees refetch's own handshake. It returns the proxy's address, or "" (and dials directly) when none is set.
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.
type Page ¶
type Page struct {
Body []byte
ContentType string
URL string // where Body came from, after redirects; "" when unknown
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.