gosoup

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2020 License: MIT Imports: 3 Imported by: 0

README

gosoup

Go Report Card GoDoc Build Status

Lightweight go library for pulling data out of HTML

it provides a convenient API for constructing and manipulating data, inspired by BeautifulSoup and Jsoup

Installation

go get -u github.com/gurkankaymak/gosoup

API Overview

Find(tagName string, attributes Attributes) *Element // returns the first occurrence of the node with the given tagName and attributes
FindByTag(tagName string) *Element // returns the first occurrence of the node with the given tagName
FindByAttributes(attributes Attributes) *Element // returns the first occurrence of the node with the given attributes

FindAll(tagName string, attributes Attributes) []*Element // returns all nodes with the given tagName and attributes
FindAllByTag(tagName string) []*Element // returns all nodes with the given tagName
FindAllByAttributes(attributes Attributes) []*Element // returns all nodes with the given attributes

Usage

element, err := gosoup.ParseAsHTML("... html as string ...")
if err != nil {
    // log/handle error
    return err
}
loginElement := element.Find("form", gosoup.Attributes{"name": "login", "class": "loginTable"})
if loginElement == nil {
    fmt.Println("could not find login element")
}
fmt.Println("loginElement:", loginElement)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes

type Attributes map[string]string

Attributes is a map[string]string that represents the attributes of an element it is used to call the Find and derivative methods conveniently as in the BeautifulSoup in example: Find("div", Attributes{"class":"exampleClass", "name":"exampleName"})

type Element

type Element struct {
	*html.Node
}

Element is the root node returned from the api methods, it contains a pointer to the html.Node the underlying *html.Node could be accessed as element.Node

func ParseAsHTML

func ParseAsHTML(input string) (*Element, error)

ParseAsHTML function parses the given string as html Returns an Element pointer to the root node and error if any error occurs

func (*Element) Find

func (element *Element) Find(tagName string, attributes Attributes) *Element

Find method returns the first occurrence of the node with the given tagName and attributes returns nil if not found any node with the given parameters

func (*Element) FindAll

func (element *Element) FindAll(tagName string, attributes Attributes) []*Element

FindAll method returns all nodes with the given tagName and attributes

func (*Element) FindAllByAttributes

func (element *Element) FindAllByAttributes(attributes Attributes) []*Element

FindAllByAttributes method returns all nodes with the given attributes

func (*Element) FindAllByTag

func (element *Element) FindAllByTag(tagName string) []*Element

FindAllByTag method returns all nodes with the given tagName

func (*Element) FindByAttributes

func (element *Element) FindByAttributes(attributes Attributes) *Element

FindByAttributes methods returns the first occurrence of the node with the given attributes

func (*Element) FindByTag

func (element *Element) FindByTag(tagName string) *Element

FindByTag method returns the first occurrence of the node with the given tagName, returns nil if not found

func (*Element) GetAttribute

func (element *Element) GetAttribute(attributeName string) (string, bool)

GetAttribute method behaves like a map lookup, returns the value of the attribute with the given key and true if found, returns "" and false if not found

func (*Element) String

func (element *Element) String() string

Returns the string representation of the parse tree, it panics if there is an error in html.Render()

Jump to

Keyboard shortcuts

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