how parse table values in golang by table id code example

Example: how parse table values in golang by table id

import (
	"fmt"
	"strings"
	"github.com/PuerkitoBio/goquery"
)
    
func Example() {    
    var clientRequest = &http.Client{
		Timeout: 3 * time.Second,
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}}
	response, err := clientRequest.PostForm(serviceURL, reqBody)

	doc, err := goquery.NewDocumentFromReader(response.Body)

	if err != nil {
		t.Fatal(err)
	}
	var person []string

	j := 0
// this wonderful package is searching into depth so be sure that
your every html elements you search for has same selector.
For Example if i've been doing it like that doc.Find("#ctl00_cphBody_gvDebtors")
There would be only one iteration into Each and this will be the String containing all the info
into this selector apartly example below contains each of td value of the table
And that's wonderful. If I was creator of the package I would write it down in the documentation
more precisely, cause now, no offence, it sucks!..

	doc.Find("#ctl00_cphBody_gvDebtors td").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		if j != 0 {
			person = append(person, strings.TrimSpace(s.Text()))
			}
		j++
	})

	fmt.Println(len(person))
}

func main(){
    Example()
}

Tags:

Go Example