1 回答

TA貢獻1793條經(jīng)驗 獲得超6個贊
它似乎適用于此:
package main
import (
"os"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
var areWeLooping = make(map[*goquery.Selection]struct{})
func getTagName(s *goquery.Selection) {
if _, weAreLooping := areWeLooping[s]; weAreLooping {
println("loop detected")
os.Exit(1)
}
areWeLooping[s] = struct{}{}
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
println(n.Data)
}
s.Children().Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Children().Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
在getTagName(s.Children())循環(huán)內(nèi)部造成了麻煩。
- 1 回答
- 0 關(guān)注
- 206 瀏覽
添加回答
舉報