英文:
How can I get all elements name on html (My bad code run loop)
问题
我想获取一个文档树。
然后,首先,我显示了所有元素的名称。
但是我的代码运行了一个循环。
我该怎么办?
package main
import (
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
func getTagName(s *goquery.Selection) {
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
println(n.Data)
getTagName(s.Children())
}
}
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
英文:
I want to get a document tree.
Then, first, I displayed all elements name.
But my code run loop.
How can I do?
package main
import (
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
func getTagName(s *goquery.Selection) {
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
println(n.Data)
getTagName(s.Children())
}
}
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
getTagName(s)
})
}
答案1
得分: 1
看起来它与以下代码一起工作:
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("检测到循环")
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())
会引起问题。
英文:
It seems to work with this:
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展开收缩; weAreLooping {
println("loop detected")
os.Exit(1)
}
areWeLooping展开收缩 = 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)
})
}
Having getTagName(s.Children())
inside the loop was causing trouble.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论