World Conquest Chronicles

World Conquest Chronicles

go-html-selector, v1.1

The library that implements collecting specified HTML tags and their attributes from an HTML document.

Friendly representation of filters and optimization of searching for the right one among them.

Change Log

  • friendly representation of filters:
    • for parsing from JSON;
    • for definition as a code literal;
  • optimizations:
    • of searching for the right one among filters;
    • of conversion from a byte slice to a string.

Features

  • collecting from an HTML document:
    • HTML tags;
    • HTML attributes;
  • options:
    • filters:
      • filtering a result:
        • by specified HTML tags;
        • by specified HTML attributes;
      • friendly representation of filters:
        • for parsing from JSON;
        • for definition as a code literal;
  • representing a result:
    • grouping HTML attributes by their tags;
  • optimizations:
    • of searching for the right one among filters;
    • of conversion from a byte slice to a string;
    • by the number:
      • of memory allocations;
      • of string copies.

Examples

htmlselector.SelectTags():

package main

import (
    "fmt"
    "log"
    "strings"

    htmlselector "github.com/thewizardplusplus/go-html-selector"
)

func main() {
    reader := strings.NewReader(`
        <ul>
            <li>
                <a href="http://example.com/1">1</a>
                <video
                    src="http://example.com/1.1"
                    poster="http://example.com/1.2">
                </video>
            </li>
            <li>
                <a href="http://example.com/2">2</a>
                <video
                    src="http://example.com/2.1"
                    poster="http://example.com/2.2">
                </video>
            </li>
        </ul>
    `)

    filters := htmlselector.OptimizeFilters(htmlselector.FilterGroup{
        "a":     {"href"},
        "video": {"src", "poster"},
    })

    tags, err := htmlselector.SelectTags(reader, filters)
    if err != nil {
        log.Fatal(err)
    }

    for _, tag := range tags {
        fmt.Printf("<%s>:\n", tag.Name)
        for _, attribute := range tag.Attributes {
            fmt.Printf("  %s=%q\n", attribute.Name, attribute.Value)
        }
    }

    // Output:
    // <a>:
    //   href="http://example.com/1"
    // <video>:
    //   src="http://example.com/1.1"
    //   poster="http://example.com/1.2"
    // <a>:
    //   href="http://example.com/2"
    // <video>:
    //   src="http://example.com/2.1"
    //   poster="http://example.com/2.2"
}

Repository

Link: https://github.com/thewizardplusplus/go-html-selector/tree/v1.1.

Content: code.

License: MIT.