Skip to content

Latest commit

 

History

History
100 lines (70 loc) · 1.95 KB

README.md

File metadata and controls

100 lines (70 loc) · 1.95 KB

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

Download the module: go get -u github.com/LeeviKopakkala/go-html-toolkit@latest

Use in your Go file: htmlutils "github.com/LeeviKopakkala/go-html-toolkit"


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"
    htmlutils "github.com/LeeviKopakkala/go-html-toolkit"
)

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.