Categorygithub.com/rbnbr/go-html-utils
module
0.0.5
Repository: https://github.com/rbnbr/go-html-utils.git
Documentation: pkg.go.dev

# README

Go-Html-Utils

Some utility functions I use for querying DOM elements which are represented as *html.Node objects.

Main Features:

  • getting nodes by condition (with multiple variations)
  • getting attributes of nodes
  • parsing a html node representing a html <table> element into a golang struct
  • parsing a html node representing a html <select> element into a golang map[string]string

Examples:

Retrieving the htmlNode

log.Printf("GET: '%s'\n", someUrl.String())
res, err := http.DefaultClient.Get(someUrl.String())
if err != nil {
	return err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
	return errors.New(fmt.Sprintf("Got status code '%v', expected '%v'.", res.StatusCode, http.StatusOK))
}

htmlNode, err = html.Parse(res.Body)
if err != nil {
	return err
}

Running queries:

Get all <div> elements

divs := GetNodesByCondition(htmlNode, MakeByTagNameCondition("div"))
if len(divs) == 0 {
	return errors.New("no divs found")
}

Get all elements containing the class name "button"

buttons := GetNodesByCondition(htmlNode, MakeByClassNameCondition("button"))
if len(divs) == 0 {
	return errors.New("no elements of class 'button' found")
}

Get first element that has attribute "name" with value "Datum"

datum := GetNodeByCondition(htmlNode, MakeByAttributeNameAndValueCondition("name", "Datum"))
if datum == nil {
	return errors.New("no element with attribute:value that is 'name':'Datum' found")
}

Combining conditions

Get first element with class name "clickable" that has as tag "button".

element := GetNodeByCondition(htmlNode, func(node *html.Node) bool {
 return MakeByClassNameCondition("clickable")(node) && MakeByTagNameCondition("button")(node)
})

Parsing html <table> elements

table := GetNodeByCondition(htmlNode, MakeByTagNameCondition("table"))
htmlTable, err := ParseHtmlTable(table, true, true, "")

dates, columnIndex, found := htmlTable.GetColumnByKey("Datum")

Parsing html <select> elements

selectElement := GetNodeByCondition(htmlNode, MakeByTagNameCondition("select"))
availableOptions, currentlySelectedOption, err := ParseSelectHTMLNode(selectElement)

log.Printf("currently selected option has text content '%s' and option value '%s'\n", currentlySelectedOption, availableOptions[currentlySelectedOption])

# Packages

No description provided by the author