Documentation
¶
Overview ¶
Colibri is an extensible web crawling and scraping framework for Go, used to crawl and extract structured data on the web.
Index ¶
- Constants
- Variables
- func AddError(errs error, key string, err error) error
- func DefaultConvFunc(key string, rawValue any) (any, error)
- func ReleaseRules(rules *Rules)
- func ReleaseSelector(selector *Selector)
- func ToURL(value any) (*url.URL, error)
- type Colibri
- type ConvFunc
- type Delay
- type Errs
- type HTTPClient
- type Parser
- type RawRules
- type Response
- type RobotsTxt
- type Rules
- type Selector
Constants ¶
const ( KeyDelay = "Delay" KeyFields = "Fields" KeyHeader = "Header" KeyIgnoreRobotsTxt = "IgnoreRobotsTxt" KeyMethod = "Method" KeyProxy = "Proxy" KeySelectors = "Selectors" KeyTimeout = "Timeout" KeyUseCookies = "UseCookies" KeyURL = "URL" )
const ( KeyAll = "All" KeyExpr = "Expr" KeyFollow = "Follow" KeyName = "Name" KeyType = "Type" )
const DefaultUserAgent = "colibri/0.1"
DefaultUserAgent is the default User-Agent used for requests.
Variables ¶
var ( // ErrClientIsNil returned when Client is nil. ErrClientIsNil = errors.New("Client is nil") // ErrParserIsNil returned when Parser is nil. ErrParserIsNil = errors.New("Parser is nil") // ErrRulesIsNil returned when rules are nil. ErrRulesIsNil = errors.New("Rules is nil") )
var ( // ErrMustBeConvBool is returned when the value is not convertible to bool. ErrMustBeConvBool = errors.New("must be a bool, string or number") // ErrMustBeConvDuration is returned when the value is not convertible to time.Duration. ErrMustBeConvDuration = errors.New("must be a string or number") // ErrMustBeString is returned when the value must be a string. ErrMustBeString = errors.New("must be a string") // ErrInvalidHeader is returned when the header is invalid. ErrInvalidHeader = errors.New("invalid header") )
var ( // ErrInvalidSelector is returned when the selector is invalid. ErrInvalidSelector = errors.New("invalid selector") // ErrInvalidSelectors is returned when the selectors are invalid. ErrInvalidSelectors = errors.New("invalid selectors") )
var ErrNotAssignable = errors.New("value is not assignable to field")
ErrNotAssignable is returned when the value of RawRules cannot be assigned to the structure field.
Functions ¶
func AddError ¶
AddError adds an error to the existing error set. If errs or err is null or the key is empty, no operation is performed. If errs is not of type *Err, a new error of type *Err is returned and the original error is stored with the key "#".
func DefaultConvFunc ¶
DefaultConvFunc ConvFunc used by default by the NewRules function.
func ReleaseRules ¶
func ReleaseRules(rules *Rules)
ReleaseRules clears and sends the rules to the rules pool.
func ReleaseSelector ¶
func ReleaseSelector(selector *Selector)
ReleaseRules clears and sends the selector to the selector pool.
Types ¶
type Colibri ¶
type Colibri struct { Client HTTPClient Delay Delay RobotsTxt RobotsTxt Parser Parser }
Colibri performs HTTP requests and parses the content of the response based on rules.
type Delay ¶
type Delay interface { // Wait waits for the previous HTTP request to the same URL and stores // the timestamp, then starts the calculated delay with the timestamp // and the specified duration of the delay. Wait(u *url.URL, duration time.Duration) // Done warns that an HTTP request has been made to the URL. Done(u *url.URL) // Stamp records the time at which the HTTP request to the URL was made. Stamp(u *url.URL) // Clear cleans the fields of the structure. Clear() }
Delay manages the delay between each HTTP request.
type Errs ¶
type Errs struct {
// contains filtered or unexported fields
}
Errs is a structure that stores and manages errors.
func (*Errs) Add ¶
Add adds an error to the error set. If the key or error is null, no operation is performed. If there is already an error stored with the same key, the error is stored with the key + # + key number. Returns a pointer to the updated error structure.
func (*Errs) Get ¶
Get returns the error associated with a key and a boolean indicating whether the key exists. If the key does not exist, a null error and false are returned.
func (*Errs) MarshalJSON ¶
MarshalJSON returns the JSON representation of the stored errors.
type HTTPClient ¶
type HTTPClient interface { // Do makes HTTP requests. Do(c *Colibri, rules *Rules) (Response, error) // Clear cleans the fields of the structure. Clear() }
HTTPClient represents an HTTP client.
type Parser ¶
type Parser interface { // Match returns true if the Content-Type is compatible with the Parser. Match(contentType string) bool // Parse parses the response based on the rules. Parse(rules *Rules, resp Response) (map[string]any, error) // Clear cleans the fields of the structure. Clear() }
Parser represents a parser of the response content.
type Response ¶
type Response interface { // URL returns the URI of the request used to obtain the response. URL() *url.URL // StatusCode returns the status code. StatusCode() int // Header returns the HTTP header of the response. Header() http.Header // Body returns the response body. Body() io.ReadCloser // Do Colibri Do method wrapper. // Wraps the Colibri used to obtain the HTTP response. Do(rules *Rules) (Response, error) // Extract Colibri Extract method wrapper. // Wraps the Colibri used to obtain the HTTP response. Extract(rules *Rules) (Response, map[string]any, error) }
Response represents an HTTP response.
type RobotsTxt ¶
type RobotsTxt interface { // IsAllowed verifies that the User-Agent can access the URL. IsAllowed(c *Colibri, rules *Rules) error // Clear cleans the fields of the structure. Clear() }
RobotsTxt represents a robots.txt parser.
type Rules ¶
type Rules struct { // Method specifies the HTTP method (GET, POST, PUT, ...). Method string // URL specifies the requested URI. URL *url.URL // Proxy specifies the proxy URI. Proxy *url.URL // Header contains the HTTP header. Header http.Header // Timeout specifies the time limit for the HTTP request. Timeout time.Duration // UseCookies specifies whether the client should send and store Cookies. UseCookies bool // IgnoreRobotsTxt specifies whether robots.txt should be ignored. IgnoreRobotsTxt bool // Delay specifies the delay time between requests. Delay time.Duration // Selectors Selectors []*Selector // Fields stores additional data. Fields map[string]any }
func NewRulesWithConvFunc ¶
NewRulesWithConvFunc returns the processed rules.
func (*Rules) Clear ¶
func (rules *Rules) Clear()
Clear clears all fields of the rules. Selectors are released, see ReleaseSelector.
func (*Rules) Clone ¶
Clone returns a copy of the original rules. Cloning the Fields field may produce errors, avoid storing pointer.
func (*Rules) UnmarshalJSON ¶
type Selector ¶
type Selector struct { // Name selector name. Name string // Expr stores the selector expression. Expr string // Type stores the type of the selector expression. Type string // All specifies whether all elements are to be found. All bool // Follow specifies whether the URLs found by the selector should be followed. Follow bool // Selectors nested selectors. Selectors []*Selector // Fields stores additional data. Fields map[string]any }
func CloneSelectors ¶
CloneSelectors clones the selectors.
func (*Selector) Clear ¶
func (selector *Selector) Clear()
Clear clears all fields of the selector. Selectors are released, see ReleaseSelector.
Directories
¶
Path | Synopsis |
---|---|
parsers are interfaces that Colibri can use to parse the content of the responses.
|
parsers are interfaces that Colibri can use to parse the content of the responses. |