Skip to content

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.

License

Notifications You must be signed in to change notification settings

LeeviKopakkala/go-html-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages