1package main 2 3import ( 4 "fmt" 5 6 "github.com/gocolly/colly/v2" 7) 8 9func main() { 10 // Instantiate default collector 11 c := colly.NewCollector( 12 // MaxDepth is 1, so only the links on the scraped page 13 // is visited, and no further links are followed 14 colly.MaxDepth(1), 15 ) 16 17 // On every a element which has href attribute call callback 18 c.OnHTML("a[href]", func(e *colly.HTMLElement) { 19 link := e.Attr("href") 20 // Print link 21 fmt.Println(link) 22 // Visit link found on page 23 e.Request.Visit(link) 24 }) 25 26 // Start scraping on https://en.wikipedia.org 27 c.Visit("https://en.wikipedia.org/") 28} 29