scrape

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 28 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 scrape fetches individual pages with reusable HTTP and browser sessions.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("scrape: client is closed")

ErrClosed indicates that the client has been closed.

View Source
var ErrSessionLost = errors.New("scrape: browser session lost")

ErrSessionLost indicates that the browser lost the client's session.

Functions

This section is empty.

Types

type Artifact added in v1.6.0

type Artifact struct {
	Name      string
	MediaType string
	Data      []byte
}

Artifact is a named representation produced from a fetched page.

type Client

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

Client owns a private cookie session. It is safe for concurrent use and admits one fetch at a time. Create a client with New.

func New

func New(opts ...Option) (*Client, error)

New creates a client. By default it uses HTTP and needs no browser installation.

func (*Client) ClearCookies

func (c *Client) ClearCookies(ctx context.Context) error

ClearCookies removes all cookies from the client's session across all hosts. It leaves the proxy and browser local storage in place. It waits for the current fetch to finish. If ctx expires during a browser command, the browser session becomes unusable and fetches and clears return ErrSessionLost until the session is reset. Sites can set fresh cookies through subsequent requests, including requests from browser service workers.

func (*Client) Close

func (c *Client) Close() error

Close cancels active and waiting operations and releases the current session. Calling Close again returns the same result.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, target string, opts ...FetchOption) (*Result, error)

Fetch performs a GET. HTTP status codes, including non-2xx codes, are returned in the result. If a later step fails, Fetch returns any available response alongside the error. For downloads, use HTTP mode. JavaScript mode returns a navigation error when Chromium treats a response as a download.

func (*Client) ResetSession

func (c *Client) ResetSession(ctx context.Context) error

ResetSession replaces the client's session. It starts with a fresh cookie jar in HTTP mode or fresh browser storage in JavaScript mode, and selects the next configured proxy. It waits for any active fetch before replacing the session, and can recover a lost browser session.

Cancellation before replacement leaves the previous session in place. If cancellation or a cleanup error occurs while closing the previous session, the new session remains active.

type FetchOption

type FetchOption func(*fetchConfig) error

FetchOption configures a single fetch.

func WaitFor

func WaitFor(selector string) FetchOption

WaitFor waits for a visible CSS selector in JavaScript mode before taking the HTML snapshot.

func WithOutputs added in v1.6.0

func WithOutputs(outputs ...Output) FetchOption

WithOutputs requests additional representations from the same navigation. Output names must be unique across all options for a fetch.

type Option

type Option func(*clientConfig) error

Option configures a client.

func WithJS

func WithJS() Option

WithJS uses a headless Chromium session. Install the matching Playwright driver and Chromium browser before creating the client.

func WithProxies

func WithProxies(proxies []string) Option

WithProxies supplies proxy endpoints. New clients select successive endpoints from the list. ResetSession advances the client's selection, and fetches keep using the selected endpoint between resets.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the time budget for a fetch, including waiting for the client to become available. The default is 30 seconds.

type Output added in v1.6.0

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

Output describes a representation to produce during Fetch.

func Markdown added in v1.6.0

func Markdown() Output

Markdown requests a text/markdown artifact named "markdown". It uses the main landmark, a single article, or the body, and omits navigation and footers. In browser mode it reads the rendered DOM after any selector wait.

type Result

type Result struct {
	Response scrapemate.Response
	// contains filtered or unexported fields
}

Result holds the original navigation response and the page's HTML representation.

func (*Result) Artifact added in v1.6.0

func (r *Result) Artifact(name string) (Artifact, bool)

Artifact returns a requested output if its conversion succeeded.

func (*Result) HTML

func (r *Result) HTML() string

HTML returns the source HTML in HTTP mode or the rendered DOM in JavaScript mode. It returns an empty string for non-HTML responses or unavailable HTML.

func (*Result) IsHTML added in v1.6.0

func (r *Result) IsHTML() bool

IsHTML reports whether the navigation response contains an HTML document.

Jump to

Keyboard shortcuts

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