forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
613 B
32 lines
613 B
2 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/gocolly/colly"
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
Max depth 最大深度
|
||
|
*/
|
||
|
func main() {
|
||
|
// 实例化默认收集器
|
||
|
c := colly.NewCollector(
|
||
|
// MaxDepth is 1, so only the links on the scraped page
|
||
|
// is visited, and no further links are followed
|
||
|
colly.MaxDepth(1),
|
||
|
)
|
||
|
|
||
|
// On every a element which has href attribute call callback
|
||
|
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
|
||
|
link := e.Attr("href")
|
||
|
// Print link
|
||
|
fmt.Println(link)
|
||
|
// Visit link found on page
|
||
|
e.Request.Visit(link)
|
||
|
})
|
||
|
|
||
|
// Start scraping on https://en.wikipedia.org
|
||
|
c.Visit("https://en.wikipedia.org/")
|
||
|
}
|