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.
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"
- 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.
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)
}
doc, err := htmlutils.UrlToHtml("https://example.com")
if err != nil {
log.Fatal(err)
}
fmt.Println("HTML from URL:", doc)
element, err := htmlutils.FindElementByTag(doc, "p")
if err != nil {
log.Fatal(err)
}
fmt.Println("Found element:", element)
element, err := htmlutils.FindElementByAttr(doc, "class", "example")
if err != nil {
log.Fatal(err)
}
fmt.Println("Found element by attribute:", element)
element, err := htmlutils.FindElementById(doc, "header")
if err != nil {
log.Fatal(err)
}
fmt.Println("Found element by ID:", element)
Functions in htmlutils return errors for invalid input or if the requested element is not found. Always handle these errors appropriately.
Feel free to contribute by submitting issues or pull requests on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.