htmlutils

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-html-toolkit

go-html-toolkit is a lightweight Go package for working with HTML documents. It provides functions to load HTML from files or URLs and search for elements using tags, attributes, or IDs.


Installation

Ensure your Go environment is set up and simply import htmlutils into your project. Use it as a local package or include it as part of your module.


Features

  • Load HTML from File: Parse an HTML file into a DOM structure.
  • Load HTML from URL: Fetch and parse HTML content from a URL.
  • Search DOM Elements:
    • By tag name.
    • By attributes.
    • By ID.

Usage

Parse HTML from File
package main

import (
    "fmt"
    "log"
    "github.com/LeeviKopakkala/go-html-toolkit/htmlutils"
)

func main() {
    doc, err := htmlutils.FileToHtml("example.html")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("HTML parsed successfully:", doc)
}
Parse HTML from URL
doc, err := htmlutils.UrlToHtml("https://example.com")
if err != nil {
    log.Fatal(err)
}
fmt.Println("HTML from URL:", doc)
Find an Element by Tag
element, err := htmlutils.FindElementByTag(doc, "p")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Found element:", element)

Find an Element by Attribute
element, err := htmlutils.FindElementByAttr(doc, "class", "example")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Found element by attribute:", element)

Find an Element by ID
element, err := htmlutils.FindElementById(doc, "header")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Found element by ID:", element)

Error handling

Functions in htmlutils return errors for invalid input or if the requested element is not found. Always handle these errors appropriately.

Contributing

Feel free to contribute by submitting issues or pull requests on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileToHtml

func FileToHtml(file string) (*html.Node, error)

func FindElementByAttr

func FindElementByAttr(n *html.Node, key, value string) (*html.Node, error)

func FindElementById

func FindElementById(n *html.Node, value string) (*html.Node, error)

func FindElementByTag

func FindElementByTag(n *html.Node, tag string) (*html.Node, error)

func UrlToHtml

func UrlToHtml(url string) (*html.Node, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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