Skip to content

hschaeidt/domquery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

domquery

Build Status

domquery is a lightweight implementation in golang for querying and searching through HTML-DOMs. Based on CSS selectors it gives the opportunity to get data and values from queried elements.

domquery is heavely based on golangs html package for searching and parsing through HTML in form of HTML Tokens. Elements returned from the lib are objects of type html.Token (golangs html package) or tokenutil.Chain (github.com/hschaeidt/domquery/tokenutil)

Usage

To use the library, first import the main package into your code

// github.com/me/application_where_to_use_domquery/main.go
package main

import (
	// ...
	"github.com/hschaeidt/domquery"
)

Then by creating a new query object it is possible to iterate through the loaded HTML

package main

import (
	// ...
	"net/http"
	"fmt"
	"github.com/hschaeidt/domquery"
)

func main() {
	// making a request to some website
	resp, err := http.Get("https://www.google.de/")

	// request successful
	if err == nil {
		// Instantiating a new query object
		q := new(domquery.Query)
		// Loading the HTML into the query
		q.Load(resp.Body)
		// Searching through HTML with CSS-selectors
		result := q.Find("{class}gb1")
		all := result.All()

		// Printing results
		for _, tokenChain := range all {
			fmt.Println(tokenChain.Value())
		}

		// Closing the request
		defer resp.Body.Close()
	}
}

About

Lightweight Go implementation for querying/searching through HTML DOMs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages