html

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: BSD-2-Clause Imports: 4 Imported by: 0

README

ptrcnull/html

library wrapping net/html with a bunch of convenience functions

example usage:

package main

import (
  "log"
  "os"

  "git.ddd.rip/ptrcnull/html"
)

func main() {
  htmlText := getSomeHtmlPage()
  document, err := html.ParseDocument(htmlText)
  if err != nil {
    log.Fatalf("parse document: %w\n", err)
  }

  table := document.QuerySelector("table")
  if table == nil {
    log.Fatalln("table not found")
  }

  if os.Getenv("DEBUG") == "1" {
    log.Println("table:", table.Render())
  }

  for _, row := range table.QuerySelectorAll("tr") {
    if strings.Contains(row.Text(), "a thing you're looking for") {
      cell := row.FindOne(func(n *html.Node) {
        return n.HasAttr("aria-label") && n.GetAttr("aria-label")[0] == "Value"
      })

      log.Println("value:", cell.TrimmedText())
    }
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	*html.Node
}

func NewTextNode

func NewTextNode(text string) *Node

func ParseDocument

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

func ParseNode

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

func (*Node) ChildNodes

func (n *Node) ChildNodes() []*Node

func (*Node) Children

func (n *Node) Children() []*Node

func (*Node) FindMany

func (n *Node) FindMany(cb func(n *Node) bool) []*Node

func (*Node) FindOne

func (n *Node) FindOne(cb func(n *Node) bool) *Node

func (*Node) ForEach

func (n *Node) ForEach(cb func(n *Node))

func (*Node) GetAttr

func (n *Node) GetAttr(key string) []string

func (*Node) GetElementByClass

func (n *Node) GetElementByClass(class string) *Node

func (*Node) GetElementById

func (n *Node) GetElementById(id string) *Node

func (*Node) GetElementByTagName

func (n *Node) GetElementByTagName(name string) *Node

func (*Node) HasAttr

func (n *Node) HasAttr(key, value string) bool

func (*Node) HasClass

func (n *Node) HasClass(class string) bool

func (*Node) QuerySelector

func (n *Node) QuerySelector(selector string) *Node

func (*Node) QuerySelectorAll

func (n *Node) QuerySelectorAll(selector string) []*Node

func (*Node) RemoveChild

func (n *Node) RemoveChild(other *Node)

func (*Node) Render

func (n *Node) Render() (string, error)

func (*Node) Text

func (n *Node) Text() string

func (*Node) Traverse

func (n *Node) Traverse(cb func(n *Node))

func (*Node) TrimmedText

func (n *Node) TrimmedText() string

Jump to

Keyboard shortcuts

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