html

package
v0.0.0-...-0a727c4 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

GOLANG HTML Parser

Parse HTML very easy.

Example:

HTML := "..."
doc, err := html.Parse(strings.NewReader(HTML))
// handle error ...

fmt.Println(doc.HTML())

element := doc.Find(&html.Match{
	Name: "p", Attributes: map[string]string{"id": "pid"},
})

fmt.Println(element.Text())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	Node *html.Node
}

func (*Element) AppendChild

func (elem *Element) AppendChild(c *Element)

AppendChild adds a element c as a child of elem.

It will panic if c already has a parent or siblings.

it is shorthand for elem.Node.AppendChild(c.Node)

func (Element) Attr

func (elem Element) Attr(name string) string

Attr returns attribute value

returns empty string if not found

func (*Element) Clear

func (elem *Element) Clear()

Clear deletes the tag from the tree of a given HTML.

func (*Element) Find

func (elem *Element) Find(selection *Match) *Element

Find returns first match

func (*Element) FindAll

func (elem *Element) FindAll(selection *Match) []*Element

FindAll returns all matches

func (Element) HTML

func (elem Element) HTML() string

HTML returns node as HTML string

func (Element) Text

func (elem Element) Text() string

type HTMLParser

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

HTML Parser

func Parse

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

Parse html reader

func (HTMLParser) Find

func (p HTMLParser) Find(selection *Match) *Element

Find returns first match

func (HTMLParser) FindAll

func (p HTMLParser) FindAll(selection *Match) []*Element

FindAll returns all matches

func (HTMLParser) FindAllFunc

func (p HTMLParser) FindAllFunc(selection *Match, f func(*Element))

func (HTMLParser) HTML

func (p HTMLParser) HTML() string

HTML returns root as HTML string

type Match

type Match struct {
	// Element name (e.g. head, title, a, div, ...)
	Name string

	// Tag attributes
	//
	// Example:
	//  map[string]string{"id":"search"}
	//  	-> <element id="search" ...>
	//
	//  map[string]string{"class":"nme"}
	//  	-> <element class="nme" ...>
	//
	// If you want only include attribute, pass empty string:
	//  map[string]string{"href":""}
	//  	-> <element href="*" ...>
	Attributes map[string]string

	// Parent of element
	//  <parent>
	//  	...
	//  	<element>
	Parent *Match

	// First child of element
	//  <element>
	//  	<child>
	FirstChild *Match
}

func (*Match) MatchNode

func (s *Match) MatchNode(node *html.Node) bool

MatchNode returns true if selection want this node

Jump to

Keyboard shortcuts

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