Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
doc, err := goquery.NewDocumentFromReader(resp.Body)

if err != nil {

    log.Fatal(err)

}


Using Find:

Code Block
doc.Find("table").Each(func(i int, sel * goquery.Selection) {

    // For sake of simplicity taking the first table of the page
    if i == 0 {

        // Looping through headers
        headers: = sel.Find("th").Each(func(_ int, sel * goquery.Selection) {
            if sel != nil {
                fmt.Print(sel.Text())
                fmt.Print(" ")
            }
        })
        fmt.Println()

        // Looping through cells
        sel.Find("td").Each(func(index int, sel * goquery.Selection) {
            if sel != nil {
                fmt.Print(sel.Text())
                fmt.Print(" ")
            }

            // Printing columns nicely
            if (index + 1) % headers.Size() == 0 {
                fmt.Println()
            }
        })
    }
})



References