2

I want to get the type name of DOM like 'a', img', 'tr', 'td', 'center' using goquery. How can I get?

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) // ?
    })
}
captncraig
  • 22,118
  • 17
  • 108
  • 151
Maiko Ohkawa
  • 853
  • 2
  • 11
  • 28

1 Answers1

5

*Selection.First gives you another *Selection which contains a slice of *html.Node which has a Data field which contains:

tag name for element nodes, content for text

So something like that:

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)
        }
    })
}
HectorJ
  • 5,814
  • 3
  • 34
  • 52
  • Oh, I see. I understood that `Data` string is tag name. Thank you for your advice. Your code is clear and safety. It's helpfull for me. Thank you. – Maiko Ohkawa Oct 17 '15 at 06:48