📌  相关文章
📜  如何通过表 id 解析 golang 中的表值 - Go 编程语言 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:45:02.648000             🧑  作者: Mango

代码示例1
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()
}