query

package module
v0.0.0-...-775479d Latest Latest
Warning

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

Go to latest
Published: May 5, 2018 License: BSD-2-Clause Imports: 10 Imported by: 11

README

html-query: A fluent and functional approach to querying HTML DOM

GoDoc

html-query is a Go package that provides a fluent and functional interface for querying HTML DOM. It is based on golang.org/x/net/html.

Examples

  1. A simple example (under "examples" directory)
    r := get(`http://blog.golang.org/index`)
    defer r.Close()
    root, err := query.Parse(r)
    checkError(err)
    root.Div(Id("content")).Children(Class("blogtitle")).For(func(item *query.Node) {
        href := item.Ahref().Href()
        date := item.Span(Class("date")).Text()
        tags := item.Span(Class("tags")).Text()
        // ......
    })
  1. Generator of html-query (under "gen" directory)

    A large part of html-query is automatically generated from HTML spec. The spec is in HTML format, so the generator parses it using html-query itself.

Design

Here is a simple explanation of the design of html-query.

Functional query expressions

All functional definitions are defined in html-query/expr package.

  1. Checker and checker composition

    A checker is a function that accept and conditionally returns a *html.Node.

    type Checker func(*html.Node) *html.Node

Here are some checker examples:

    Id("id1")
    Class("c1")
    Div
    Abbr
    H1
    H2

Checkers can be combined as boolean expressions:

    And(Id("id1"), Class("c1"))
    Or(Class("c1"), Class("c2"))
    And(Class("c1"), Not(Class("c2")))
  1. Checker builder

    A checker builder is a function that returns a checker. "Id", "Class", "And", "Or", "Not" shown above are all checker builders. There are also some checker builder builder (function that returns a checker builder) defined in html-query when needed.

Fluent interface

Fluent interface (http://en.wikipedia.org/wiki/Fluent_interface) are defined in html-query package.

  1. Root node

    Function Parse returns the root node of an html document.

  2. Node finder

    Method Node.Find implements a BFS search for a node, e.g.

    node.Find(Div, Class("id1"))

But usually you can write the short form:

    node.Div(Class("id1"))
  1. Attribute getter

    Method Node.Attr can be used to get the value (or a regular expression submatch of the value) of a node, e.g.

    node.Attr("Id")
    node.Attr("href", "\(.*)")

But usually you can write the short form:

    node.Id()
    node.Href("\(.*)")
  1. Node iterator

    Method Node.Children and Node.Descendants each returns a node iterator (NodeIter). Method NodeIter.For can be used to loop through these nodes.

Alternative

If you prefer a jquery like DSL rather than functional way, you might want to try goquery: https://github.com/PuerkitoBio/goquery.

Documentation

Overview

Package query provides a fluent and functional interface for querying HTML DOM using Go. It is based on golang.org/x/net/html.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

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

Node represents a HTML node. Wrap html.Node so that chainable interface is possible Use pointer of it because we want to test with nil.

func NewNode

func NewNode(n *html.Node) *Node

func Parse

func Parse(r io.Reader) (*Node, error)

func (*Node) A

func (n *Node) A(cs ...Checker) *Node

func (*Node) Abbr

func (n *Node) Abbr(cs ...Checker) *Node

func (*Node) Abbr_

func (n *Node) Abbr_(pat ...string) *string

func (*Node) Accept

func (n *Node) Accept(pat ...string) *string

func (*Node) AcceptCharset

func (n *Node) AcceptCharset(pat ...string) *string

func (*Node) Accesskey

func (n *Node) Accesskey(pat ...string) *string

func (*Node) Action

func (n *Node) Action(pat ...string) *string

func (*Node) Address

func (n *Node) Address(cs ...Checker) *Node

func (*Node) Ahref

func (n *Node) Ahref(cs ...Checker) *Node

func (*Node) AllText

func (n *Node) AllText(pat ...string) *string

func (*Node) Allowfullscreen

func (n *Node) Allowfullscreen(pat ...string) *string

func (*Node) Allowpaymentrequest

func (n *Node) Allowpaymentrequest(pat ...string) *string

func (*Node) Allowusermedia

func (n *Node) Allowusermedia(pat ...string) *string

func (*Node) Alt

func (n *Node) Alt(pat ...string) *string

func (*Node) Area

func (n *Node) Area(cs ...Checker) *Node

func (*Node) Article

func (n *Node) Article(cs ...Checker) *Node

func (*Node) As

func (n *Node) As(pat ...string) *string

func (*Node) Aside

func (n *Node) Aside(cs ...Checker) *Node

func (*Node) Async

func (n *Node) Async(pat ...string) *string

func (*Node) Attr

func (n *Node) Attr(key string, pat ...string) *string

func (*Node) Audio

func (n *Node) Audio(cs ...Checker) *Node

func (*Node) Autocomplete

func (n *Node) Autocomplete(pat ...string) *string

func (*Node) Autofocus

func (n *Node) Autofocus(pat ...string) *string

func (*Node) Autoplay

func (n *Node) Autoplay(pat ...string) *string

func (*Node) B

func (n *Node) B(cs ...Checker) *Node

func (*Node) Base

func (n *Node) Base(cs ...Checker) *Node

func (*Node) Bdi

func (n *Node) Bdi(cs ...Checker) *Node

func (*Node) Bdo

func (n *Node) Bdo(cs ...Checker) *Node

func (*Node) Blockquote

func (n *Node) Blockquote(cs ...Checker) *Node

func (*Node) Body

func (n *Node) Body(cs ...Checker) *Node

func (*Node) Br

func (n *Node) Br(cs ...Checker) *Node

func (*Node) Button

func (n *Node) Button(cs ...Checker) *Node

func (*Node) Canvas

func (n *Node) Canvas(cs ...Checker) *Node

func (*Node) Caption

func (n *Node) Caption(cs ...Checker) *Node

func (*Node) Charset

func (n *Node) Charset(pat ...string) *string

func (*Node) Checked

func (n *Node) Checked(pat ...string) *string

func (*Node) Children

func (n *Node) Children(cs ...Checker) NodeIter

func (*Node) Cite

func (n *Node) Cite(cs ...Checker) *Node

func (*Node) Cite_

func (n *Node) Cite_(pat ...string) *string

func (*Node) Class

func (n *Node) Class(pat ...string) *string

func (*Node) Code

func (n *Node) Code(cs ...Checker) *Node

func (*Node) Col

func (n *Node) Col(cs ...Checker) *Node

func (*Node) Colgroup

func (n *Node) Colgroup(cs ...Checker) *Node

func (*Node) Color

func (n *Node) Color(pat ...string) *string

func (*Node) Cols

func (n *Node) Cols(pat ...string) *string

func (*Node) Colspan

func (n *Node) Colspan(pat ...string) *string

func (*Node) Content

func (n *Node) Content(pat ...string) *string

func (*Node) Contenteditable

func (n *Node) Contenteditable(pat ...string) *string

func (*Node) Controls

func (n *Node) Controls(pat ...string) *string

func (*Node) Coords

func (n *Node) Coords(pat ...string) *string

func (*Node) Crossorigin

func (n *Node) Crossorigin(pat ...string) *string

func (*Node) Data

func (n *Node) Data(cs ...Checker) *Node

func (*Node) Data_

func (n *Node) Data_(pat ...string) *string

func (*Node) Datalist

func (n *Node) Datalist(cs ...Checker) *Node

func (*Node) Datetime

func (n *Node) Datetime(pat ...string) *string

func (*Node) Dd

func (n *Node) Dd(cs ...Checker) *Node

func (*Node) Default

func (n *Node) Default(pat ...string) *string

func (*Node) Defer

func (n *Node) Defer(pat ...string) *string

func (*Node) Del

func (n *Node) Del(cs ...Checker) *Node

func (*Node) Descendants

func (n *Node) Descendants(cs ...Checker) NodeIter

func (*Node) Details

func (n *Node) Details(cs ...Checker) *Node

func (*Node) Dfn

func (n *Node) Dfn(cs ...Checker) *Node

func (*Node) Dialog

func (n *Node) Dialog(cs ...Checker) *Node

func (*Node) Dir

func (n *Node) Dir(pat ...string) *string

func (*Node) Dirname

func (n *Node) Dirname(pat ...string) *string

func (*Node) Disabled

func (n *Node) Disabled(pat ...string) *string

func (*Node) Div

func (n *Node) Div(cs ...Checker) *Node

func (*Node) Dl

func (n *Node) Dl(cs ...Checker) *Node

func (*Node) Download

func (n *Node) Download(pat ...string) *string

func (*Node) Draggable

func (n *Node) Draggable(pat ...string) *string

func (*Node) Dt

func (n *Node) Dt(cs ...Checker) *Node

func (*Node) Em

func (n *Node) Em(cs ...Checker) *Node

func (*Node) Embed

func (n *Node) Embed(cs ...Checker) *Node

func (*Node) Enctype

func (n *Node) Enctype(pat ...string) *string

func (*Node) Fieldset

func (n *Node) Fieldset(cs ...Checker) *Node

func (*Node) Figcaption

func (n *Node) Figcaption(cs ...Checker) *Node

func (*Node) Figure

func (n *Node) Figure(cs ...Checker) *Node

func (*Node) Find

func (n *Node) Find(cs ...Checker) *Node

func (*Node) FindChild

func (n *Node) FindChild(cs ...Checker) *Node

func (*Node) FindNext

func (n *Node) FindNext(cs ...Checker) *Node

func (*Node) Footer

func (n *Node) Footer(cs ...Checker) *Node

func (*Node) For

func (n *Node) For(pat ...string) *string

func (*Node) Form

func (n *Node) Form(cs ...Checker) *Node

func (*Node) Form_

func (n *Node) Form_(pat ...string) *string

func (*Node) Formaction

func (n *Node) Formaction(pat ...string) *string

func (*Node) Formenctype

func (n *Node) Formenctype(pat ...string) *string

func (*Node) Formmethod

func (n *Node) Formmethod(pat ...string) *string

func (*Node) Formnovalidate

func (n *Node) Formnovalidate(pat ...string) *string

func (*Node) Formtarget

func (n *Node) Formtarget(pat ...string) *string

func (*Node) H1

func (n *Node) H1(cs ...Checker) *Node

func (*Node) H2

func (n *Node) H2(cs ...Checker) *Node

func (*Node) H3

func (n *Node) H3(cs ...Checker) *Node

func (*Node) H4

func (n *Node) H4(cs ...Checker) *Node

func (*Node) H5

func (n *Node) H5(cs ...Checker) *Node

func (*Node) H6

func (n *Node) H6(cs ...Checker) *Node

func (*Node) Head

func (n *Node) Head(cs ...Checker) *Node

func (*Node) Header

func (n *Node) Header(cs ...Checker) *Node

func (*Node) Headers

func (n *Node) Headers(pat ...string) *string

func (*Node) Height

func (n *Node) Height(pat ...string) *string

func (*Node) Hgroup

func (n *Node) Hgroup(cs ...Checker) *Node

func (*Node) Hidden

func (n *Node) Hidden(pat ...string) *string

func (*Node) High

func (n *Node) High(pat ...string) *string

func (*Node) Hr

func (n *Node) Hr(cs ...Checker) *Node

func (*Node) Href

func (n *Node) Href(pat ...string) *string

func (*Node) Hreflang

func (n *Node) Hreflang(pat ...string) *string

func (*Node) Html

func (n *Node) Html(cs ...Checker) *Node

func (*Node) HttpEquiv

func (n *Node) HttpEquiv(pat ...string) *string

func (*Node) I

func (n *Node) I(cs ...Checker) *Node

func (*Node) Id

func (n *Node) Id(pat ...string) *string

func (*Node) Iframe

func (n *Node) Iframe(cs ...Checker) *Node

func (*Node) Img

func (n *Node) Img(cs ...Checker) *Node

func (*Node) Input

func (n *Node) Input(cs ...Checker) *Node

func (*Node) Inputmode

func (n *Node) Inputmode(pat ...string) *string

func (*Node) Ins

func (n *Node) Ins(cs ...Checker) *Node

func (*Node) Integrity

func (n *Node) Integrity(pat ...string) *string

func (*Node) InternalNode

func (n *Node) InternalNode() *html.Node

func (*Node) Is

func (n *Node) Is(pat ...string) *string

func (*Node) Ismap

func (n *Node) Ismap(pat ...string) *string

func (*Node) Itemid

func (n *Node) Itemid(pat ...string) *string

func (*Node) Itemprop

func (n *Node) Itemprop(pat ...string) *string

func (*Node) Itemref

func (n *Node) Itemref(pat ...string) *string

func (*Node) Itemscope

func (n *Node) Itemscope(pat ...string) *string

func (*Node) Itemtype

func (n *Node) Itemtype(pat ...string) *string

func (*Node) Kbd

func (n *Node) Kbd(cs ...Checker) *Node

func (*Node) Kind

func (n *Node) Kind(pat ...string) *string

func (*Node) Label

func (n *Node) Label(cs ...Checker) *Node

func (*Node) Label_

func (n *Node) Label_(pat ...string) *string

func (*Node) Lang

func (n *Node) Lang(pat ...string) *string

func (*Node) Legend

func (n *Node) Legend(cs ...Checker) *Node

func (*Node) Li

func (n *Node) Li(cs ...Checker) *Node
func (n *Node) Link(cs ...Checker) *Node

func (*Node) List

func (n *Node) List(pat ...string) *string

func (*Node) Loop

func (n *Node) Loop(pat ...string) *string

func (*Node) Low

func (n *Node) Low(pat ...string) *string

func (*Node) Manifest

func (n *Node) Manifest(pat ...string) *string

func (*Node) Map

func (n *Node) Map(cs ...Checker) *Node

func (*Node) Mark

func (n *Node) Mark(cs ...Checker) *Node

func (*Node) Max

func (n *Node) Max(pat ...string) *string

func (*Node) Maxlength

func (n *Node) Maxlength(pat ...string) *string

func (*Node) Media

func (n *Node) Media(pat ...string) *string

func (*Node) Menu

func (n *Node) Menu(cs ...Checker) *Node

func (*Node) Meta

func (n *Node) Meta(cs ...Checker) *Node

func (*Node) Meter

func (n *Node) Meter(cs ...Checker) *Node

func (*Node) Method

func (n *Node) Method(pat ...string) *string

func (*Node) Min

func (n *Node) Min(pat ...string) *string

func (*Node) Minlength

func (n *Node) Minlength(pat ...string) *string

func (*Node) Multiple

func (n *Node) Multiple(pat ...string) *string

func (*Node) Muted

func (n *Node) Muted(pat ...string) *string

func (*Node) Name

func (n *Node) Name(pat ...string) *string

func (*Node) Nav

func (n *Node) Nav(cs ...Checker) *Node

func (*Node) NextSibling

func (n *Node) NextSibling() *Node

func (*Node) Nomodule

func (n *Node) Nomodule(pat ...string) *string

func (*Node) Nonce

func (n *Node) Nonce(pat ...string) *string

func (*Node) Noscript

func (n *Node) Noscript(cs ...Checker) *Node

func (*Node) Novalidate

func (n *Node) Novalidate(pat ...string) *string

func (*Node) Object

func (n *Node) Object(cs ...Checker) *Node

func (*Node) Ol

func (n *Node) Ol(cs ...Checker) *Node

func (*Node) Open

func (n *Node) Open(pat ...string) *string

func (*Node) Optgroup

func (n *Node) Optgroup(cs ...Checker) *Node

func (*Node) Optimum

func (n *Node) Optimum(pat ...string) *string

func (*Node) Option

func (n *Node) Option(cs ...Checker) *Node

func (*Node) Output

func (n *Node) Output(cs ...Checker) *Node

func (*Node) P

func (n *Node) P(cs ...Checker) *Node

func (*Node) Param

func (n *Node) Param(cs ...Checker) *Node

func (*Node) Parent

func (n *Node) Parent() *Node

func (*Node) Pattern

func (n *Node) Pattern(pat ...string) *string

func (*Node) Ping

func (n *Node) Ping(pat ...string) *string

func (*Node) Placeholder

func (n *Node) Placeholder(pat ...string) *string

func (*Node) PlainText

func (n *Node) PlainText() *string

func (*Node) Playsinline

func (n *Node) Playsinline(pat ...string) *string

func (*Node) Poster

func (n *Node) Poster(pat ...string) *string

func (*Node) Pre

func (n *Node) Pre(cs ...Checker) *Node

func (*Node) Preload

func (n *Node) Preload(pat ...string) *string

func (*Node) PrettyPrint

func (n *Node) PrettyPrint()

func (*Node) PrettyRender

func (n *Node) PrettyRender(w io.Writer, indentSize int) error

PrettyRender renders prettily the parse tree n to the given writer, for easily viewing as plain text.

func (*Node) PrevSibling

func (n *Node) PrevSibling() *Node

func (*Node) Progress

func (n *Node) Progress(cs ...Checker) *Node

func (*Node) Q

func (n *Node) Q(cs ...Checker) *Node

func (*Node) Readonly

func (n *Node) Readonly(pat ...string) *string

func (*Node) Referrerpolicy

func (n *Node) Referrerpolicy(pat ...string) *string

func (*Node) Rel

func (n *Node) Rel(pat ...string) *string

func (*Node) Render

func (n *Node) Render() *string

func (*Node) RenderChildren

func (n *Node) RenderChildren() *string

func (*Node) RenderTagOnly

func (n *Node) RenderTagOnly() *string

func (*Node) Required

func (n *Node) Required(pat ...string) *string

func (*Node) Reversed

func (n *Node) Reversed(pat ...string) *string

func (*Node) Rows

func (n *Node) Rows(pat ...string) *string

func (*Node) Rowspan

func (n *Node) Rowspan(pat ...string) *string

func (*Node) Rp

func (n *Node) Rp(cs ...Checker) *Node

func (*Node) Rt

func (n *Node) Rt(cs ...Checker) *Node

func (*Node) Ruby

func (n *Node) Ruby(cs ...Checker) *Node

func (*Node) S

func (n *Node) S(cs ...Checker) *Node

func (*Node) Samp

func (n *Node) Samp(cs ...Checker) *Node

func (*Node) Sandbox

func (n *Node) Sandbox(pat ...string) *string

func (*Node) Satisfy

func (n *Node) Satisfy(cs ...Checker) bool

func (*Node) Scope

func (n *Node) Scope(pat ...string) *string

func (*Node) Script

func (n *Node) Script(cs ...Checker) *Node

func (*Node) Section

func (n *Node) Section(cs ...Checker) *Node

func (*Node) Select

func (n *Node) Select(cs ...Checker) *Node

func (*Node) Selected

func (n *Node) Selected(pat ...string) *string

func (*Node) Shape

func (n *Node) Shape(pat ...string) *string

func (*Node) Size

func (n *Node) Size(pat ...string) *string

func (*Node) Sizes

func (n *Node) Sizes(pat ...string) *string

func (*Node) Slot_

func (n *Node) Slot_(pat ...string) *string

func (*Node) Small

func (n *Node) Small(cs ...Checker) *Node

func (*Node) Source

func (n *Node) Source(cs ...Checker) *Node

func (*Node) Span

func (n *Node) Span(cs ...Checker) *Node

func (*Node) Span_

func (n *Node) Span_(pat ...string) *string

func (*Node) Spellcheck

func (n *Node) Spellcheck(pat ...string) *string

func (*Node) Src

func (n *Node) Src(pat ...string) *string

func (*Node) Srcdoc

func (n *Node) Srcdoc(pat ...string) *string

func (*Node) Srclang

func (n *Node) Srclang(pat ...string) *string

func (*Node) Srcset

func (n *Node) Srcset(pat ...string) *string

func (*Node) Start

func (n *Node) Start(pat ...string) *string

func (*Node) Step

func (n *Node) Step(pat ...string) *string

func (*Node) Strong

func (n *Node) Strong(cs ...Checker) *Node

func (*Node) Style

func (n *Node) Style(cs ...Checker) *Node

func (*Node) Style_

func (n *Node) Style_(pat ...string) *string

func (*Node) Sub

func (n *Node) Sub(cs ...Checker) *Node

func (*Node) Summary

func (n *Node) Summary(cs ...Checker) *Node

func (*Node) Sup

func (n *Node) Sup(cs ...Checker) *Node

func (*Node) Tabindex

func (n *Node) Tabindex(pat ...string) *string

func (*Node) Table

func (n *Node) Table(cs ...Checker) *Node

func (*Node) Target

func (n *Node) Target(pat ...string) *string

func (*Node) Tbody

func (n *Node) Tbody(cs ...Checker) *Node

func (*Node) Td

func (n *Node) Td(cs ...Checker) *Node

func (*Node) Text

func (n *Node) Text(pat ...string) *string

func (*Node) TextNode

func (n *Node) TextNode(pat string) *TextNodeNode

func (*Node) Textarea

func (n *Node) Textarea(cs ...Checker) *Node

func (*Node) Tfoot

func (n *Node) Tfoot(cs ...Checker) *Node

func (*Node) Th

func (n *Node) Th(cs ...Checker) *Node

func (*Node) Thead

func (n *Node) Thead(cs ...Checker) *Node

func (*Node) Time

func (n *Node) Time(cs ...Checker) *Node

func (*Node) Title

func (n *Node) Title(cs ...Checker) *Node

func (*Node) Title_

func (n *Node) Title_(pat ...string) *string

func (*Node) Tr

func (n *Node) Tr(cs ...Checker) *Node

func (*Node) Track

func (n *Node) Track(cs ...Checker) *Node

func (*Node) Translate

func (n *Node) Translate(pat ...string) *string

func (*Node) Type

func (n *Node) Type(pat ...string) *string

func (*Node) Typemustmatch

func (n *Node) Typemustmatch(pat ...string) *string

func (*Node) U

func (n *Node) U(cs ...Checker) *Node

func (*Node) Ul

func (n *Node) Ul(cs ...Checker) *Node

func (*Node) Updateviacache

func (n *Node) Updateviacache(pat ...string) *string

func (*Node) Usemap

func (n *Node) Usemap(pat ...string) *string

func (*Node) Value

func (n *Node) Value(pat ...string) *string

func (*Node) Var

func (n *Node) Var(cs ...Checker) *Node

func (*Node) Video

func (n *Node) Video(cs ...Checker) *Node

func (*Node) Wbr

func (n *Node) Wbr(cs ...Checker) *Node

func (*Node) Width

func (n *Node) Width(pat ...string) *string

func (*Node) Workertype

func (n *Node) Workertype(pat ...string) *string

func (*Node) Wrap

func (n *Node) Wrap(pat ...string) *string

type NodeIter

type NodeIter struct {
	Iter
}

func (NodeIter) A

func (i NodeIter) A(cs ...Checker) NodeIter

func (NodeIter) All

func (i NodeIter) All() (nodes []*Node)

func (NodeIter) Div

func (i NodeIter) Div(cs ...Checker) NodeIter

func (NodeIter) Find

func (i NodeIter) Find(cs ...Checker) NodeIter

func (NodeIter) For

func (i NodeIter) For(visit func(n *Node))

func (NodeIter) H2

func (i NodeIter) H2(cs ...Checker) NodeIter

func (NodeIter) H3

func (i NodeIter) H3(cs ...Checker) NodeIter

func (NodeIter) H4

func (i NodeIter) H4(cs ...Checker) NodeIter

func (NodeIter) Href

func (i NodeIter) Href(pat ...string) []string

func (NodeIter) Integers

func (i NodeIter) Integers(f StringGetter) []int

func (NodeIter) Next

func (i NodeIter) Next() *Node

func (NodeIter) Strings

func (i NodeIter) Strings(f StringGetter, pat ...string) []string

func (NodeIter) Td

func (i NodeIter) Td(cs ...Checker) NodeIter

type NodeStack

type NodeStack struct {
	*Stack
}

type TextNodeNode

type TextNodeNode struct {
	Node
	// contains filtered or unexported fields
}

func NewTextNodeNode

func NewTextNodeNode(n *Node, rx *regexp.Regexp) *TextNodeNode

func (*TextNodeNode) Submatch

func (n *TextNodeNode) Submatch() *string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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