Code Monkey home page Code Monkey logo

go-html-utils's Introduction

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")
}

Create arbitrary conditions & Combining conditions

Create arbitrary condition

element := GetNodeByCondition(htmlNode, func(node *html.Node) bool {
	boolean := true
	/*
	Some arbitrary logic.
	The @node parameter takes the value of all *html.Node elements in the tree of @htmlNode for which this function is evaluated.
	GetNodeByCondition will return the first node, for which this function evaluates true.
	 */
	return boolean
})

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

Defining custom conditions also allows to combine existing conditions:

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])

go-html-utils's People

Contributors

rbnbr avatar

Stargazers

AKnep avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.