scan

package
v0.0.0-...-bba8168 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package scan implements structures that can scan HTML into go values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attr

func Attr(n *html.Node, key string) (val string, ok bool)

Attr returns an attribute value by its key.

`ok` is true if the value exists.

func Text

func Text(n *html.Node) string

Text returns the inner text of n.

Types

type Options

type Options struct {
	Selector cascadia.Selector
	Attr     string
}

Options represents the scan options.

type ScanFunc

type ScanFunc func(dst reflect.Value, n *html.Node) error

ScanFunc represents a scanner func.

The function receives a destination to scan into and the source as the root html node.

Scan functions can be nested, for example a slice scan func will typically nest its element types, a struct scan func will nest its field types.

func Bytes

func Bytes(opts Options) ScanFunc

Bytes returns a scanner func for a byte slice.

func Float

func Float(opts Options) ScanFunc

Float returns a scanner func for a float.

func Int

func Int(opts Options) ScanFunc

Int returns a scanner func for an int.

func ScannerOf

func ScannerOf(t reflect.Type, opts Options) (ScanFunc, error)

ScannerOf returns a scanner of type t using opts.

If the type is not supported, the method returns an error with type information.

Some types which define tags will return an error if the tag cannot be compiled.

func Slice

func Slice(opts Options, t reflect.Type) (ScanFunc, error)

Slice returns a new slice scanner.

func String

func String(opts Options) ScanFunc

String returns a scanner func for a string.

func Struct

func Struct(opts Options, t reflect.Type) (ScanFunc, error)

Struct returns a scanfunc for a struct or an error.

func Uint

func Uint(opts Options) ScanFunc

Uint returns a scanner func for an uint.

type Scanner

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

Scanner implements a type scanner.

The scanner extracts raw HTML data into registered types. When the scanner sees a new type it will analyze it and create a scanfunc that can be re-used with different html nodes.

The scanner supports the follow types:

  • int
  • uint
  • float
  • string
  • byte
  • struct

The scanner also supports exporting multiple html nodes into a slice of the above types.

The scanner uses tags to lookup data in the root html node and extract its data, for example:

AuthorURLs []string `css:"a.author@href"`

Will export all hrefs from anchor tags that have the class `.author`.

func NewScanner

func NewScanner() *Scanner

NewScanner returns a new scanner.

func (*Scanner) Scan

func (s *Scanner) Scan(dst interface{}, src *html.Node, opts Options) error

Scan scans from src node into dst value.

Jump to

Keyboard shortcuts

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