Files
request/v2/gohtml.go
Matheus Sampaio Queiroga c27e2c639c
All checks were successful
Golang test / go-test (push) Successful in 13s
Add new methods and update gohtml
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2025-03-13 19:25:25 -03:00

42 lines
1.1 KiB
Go

package request
import (
"net/http"
"reflect"
"sirherobrine23.com.br/go-bds/request/gohtml"
"github.com/PuerkitoBio/goquery"
)
// Process html document and return [*github.com/PuerkitoBio/goquery.Document]
func GoDocument(Url string, Option *Options) (doc *goquery.Document, res *http.Response, err error) {
if res, err = Request(Url, Option); err == nil {
defer res.Body.Close()
if doc, err = goquery.NewDocumentFromReader(res.Body); err == nil {
doc.Url = res.Request.URL
}
}
return
}
// Make request and parse HTML pages/content
func GoHTML[T any](Url string, Option *Options) (v T, res *http.Response, err error) {
res, err = DoGoHTML(Url, &v, Option)
return v, res, err
}
// Make request and append data to current struct
func DoGoHTML(Url string, v any, Option *Options) (*http.Response, error) {
doc, res, err := GoDocument(Url, Option)
if err != nil {
return res, err
}
valueof := reflect.ValueOf(v)
if err := gohtml.CheckTarget(valueof); err != nil {
return res, err
}
return res, gohtml.QueryParse(doc.Selection, valueof, gohtml.TextExtractor)
}