...
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
Reference | URL |
---|---|
Find out how to scrape HTML tables with Golang | https://www.webscrapingapi.com/find-out-how-to-scrape-html-tables-with-golang |