Skip to content

最大深度


go
package main

import (
	"fmt"
	"github.com/gocolly/colly"
)

func main() {
	c := colly.NewCollector(
		// 最大深度为1,则只会爬取当前页面的所有链接
		colly.MaxDepth(1),
	)

	// 每个有href的元素回调
	c.OnHTML("a[href]", func(e *colly.HTMLElement) {
		link := e.Attr("href")
		// 打印链接
		fmt.Println(link)
		// 访问爬到的链接
		e.Request.Visit(link)
	})

	// 开始爬取 https://en.wikipedia.org
	c.Visit("https://en.wikipedia.org/")
}