Skip to content

wmentor/html

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTML

Coverage Status https://goreportcard.com/report/github.com/wmentor/html https://pkg.go.dev/github.com/wmentor/html License: MIT

Simple HTML parser and data fetcher library written on Golang under MIT License.

Require

  • Golang (version >= 1.20)
  • golang.org/x/net

Install

go get github.com/wmentor/html

Usage

Fetch data from URL

package main

import (
  "fmt"
  "time"

  "github.com/wmentor/html"
)

func main() {

  src := "https://edition.cnn.com"

  parser := html.New()

  opts := &html.GetOpts{
    Agent:"Mozilla/5.0 (compatible; MSIE 10.0)",
    Timeout: time.Second*60,
  }

  parser.Get(src,opts)
  fmt.Println( string(parser.Text()) )

  parser.EachLink(func(link string) {
    fmt.Println("url=" + link)
  } )

  parser.EachImage(func(link string) {
    fmt.Println("img=" + link)
  } )

  parser.EachIframe(func(link string) {
    fmt.Println("iframe=" + link)
  } )
}

Fetch data from file/stdin

package main

import (
  "fmt"
  "os"

  "github.com/wmentor/html"
)

func main() {

  parser := html.New()

  parser.Parse(os.Stdin) // io.Reader
  fmt.Println( string(parser.Text()) )

  parser.EachLink(func(link string) {
    fmt.Println("url=" + link)
  } )

  parser.EachImage(func(link string) {
    fmt.Println("img=" + link)
  } )

  parser.EachIframe(func(link string) {
    fmt.Println("iframe=" + link)
  } )
}

Packages

No packages published

Languages