goxtag

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 9 Imported by: 0

README

goxtag

GitHub go.mod Go version Build Status Coverage Status

This package is an analog of github.com/andrewstuart/goq for xpath selectors.

Install

go get -u github.com/azlotnikov/goxtag

Example

package main

import (
    "github.com/azlotnikov/goxtag"
    "log"
    "net/http"
)

// Structured representation for github file name table
type example struct {
    Title string `xpath:"//h1"`
    Files []string `xpath:".//table[contains(concat(' ',normalize-space(@class),' '),' files ')]//tbody//tr[contains(concat(' ',normalize-space(@class),' '),' js-navigation-item ')]//td[contains(concat(' ',normalize-space(@class),' '),' content ')]"`
}

func main() {
    res, err := http.Get("https://github.com/azlotnikov/goxtag")
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()

    var ex example
	
    err = goxtag.NewDecoder(res.Body).Decode(&ex)
    if err != nil {
        log.Fatal(err)
    }

    log.Println(ex.Title, ex.Files)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TypeDeref

func TypeDeref(t reflect.Type) reflect.Type

TypeDeref returns the underlying type if the given type is a pointer.

func Unmarshal

func Unmarshal(bs []byte, v interface{}) error

Unmarshal takes a byte slice and a destination pointer to any interface{}, and unmarshals the document into the destination based on the rules above. Any error returned here will likely be of type CannotUnmarshalError, though an initial htmlquery error will pass through directly.

func UnmarshalSelection

func UnmarshalSelection(doc *Document, iface interface{}) error

Types

type CannotUnmarshalError

type CannotUnmarshalError struct {
	Err      error
	Val      string
	FldOrIdx interface{}
	V        reflect.Value
	Reason   string
	XPath    string
}

CannotUnmarshalError represents an error returned by the goqxtag Unmarshaler and helps consumers in programmatically diagnosing the cause of their error.

func (*CannotUnmarshalError) Error

func (e *CannotUnmarshalError) Error() string

type Decoder

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

Decoder implements the same API you will see in encoding/xml and encoding/json except that we do not currently support proper streaming decoding as it is not supported by goquery upstream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder given an io.Reader

func (*Decoder) Decode

func (d *Decoder) Decode(dest interface{}) error

Decode will unmarshal the contents of the decoder when given an instance of an annotated type as its argument. It will return any errors encountered during either parsing the document or unmarshaling into the given object.

type Document

type Document struct {
	Nodes []*html.Node
}

func NewDocumentWithNode added in v1.3.0

func NewDocumentWithNode(node *html.Node) *Document

func NewDocumentWithNodes added in v1.3.0

func NewDocumentWithNodes(nodes []*html.Node) *Document

func (*Document) Attr

func (doc *Document) Attr(attrName string) (val string, exists bool)

func (*Document) Eq

func (doc *Document) Eq(index int) *Document

func (*Document) Find

func (doc *Document) Find(selector string) *Document

func (*Document) FindOne added in v1.2.0

func (doc *Document) FindOne(selector string) (*Document, error)

func (*Document) Html

func (doc *Document) Html() (ret string, e error)

func (*Document) IsEmpty added in v1.2.0

func (doc *Document) IsEmpty() bool

func (*Document) Length

func (doc *Document) Length() int

func (*Document) Slice

func (doc *Document) Slice(start, end int) *Document

func (*Document) Text

func (doc *Document) Text() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalHTML([]*html.Node) error
}

Jump to

Keyboard shortcuts

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