如何使用 goquery 获取 DOM 的类型名称?
How can I get the type name of DOM using goquery?
我想使用 goquery 获取 DOM 的类型名称,如 'a'、img'、'tr'、'td'、'center'。
我怎样才能得到?
package main
import (
"github.com/PuerkitoBio/goquery"
)
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
// for debug.
println(s.Size()) // return 1
// I expect '<center>' on this URL, but I can't get it's name.
// println(s.First().xxx) // ?
})
}
*Selection.First
gives you another *Selection
which contains a slice of *html.Node
有一个 Data
字段,其中包含:
tag name for element nodes, content for text
类似这样的事情:
package main
import (
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
// for debug.
println(s.Size()) // return 1
if len(s.Nodes) > 0 && s.Nodes[0].Type == html.ElementNode {
println(s.Nodes[0].Data)
}
})
}
我想使用 goquery 获取 DOM 的类型名称,如 'a'、img'、'tr'、'td'、'center'。 我怎样才能得到?
package main
import (
"github.com/PuerkitoBio/goquery"
)
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
// for debug.
println(s.Size()) // return 1
// I expect '<center>' on this URL, but I can't get it's name.
// println(s.First().xxx) // ?
})
}
*Selection.First
gives you another *Selection
which contains a slice of *html.Node
有一个 Data
字段,其中包含:
tag name for element nodes, content for text
类似这样的事情:
package main
import (
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
func main() {
doc, _ := goquery.NewDocument("https://news.ycombinator.com/")
doc.Find("html body").Each(func(_ int, s *goquery.Selection) {
// for debug.
println(s.Size()) // return 1
if len(s.Nodes) > 0 && s.Nodes[0].Type == html.ElementNode {
println(s.Nodes[0].Data)
}
})
}