On Thu, Jan 9, 2014 at 8:00 AM, t0 <
cod...@gmail.com> wrote:
> I can't seem to figure out how to get the text data between tags.
Text is the Data of TextNodes, not a property of ElementNodes. A HTML
element can contain more than one text node. Note that I put a <b>
element in your <span> element in the code below.
s := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a
href="/bar/baz">BarBaz</a></ul><span>TEXT <b>I</b> WANT</span>`
doc, err := html.Parse(strings.NewReader(s))
if err != nil {
log.Fatal(err)
}
var f func(*html.Node, bool)
f = func(n *html.Node, printText bool) {
if printText && n.Type == html.TextNode {
fmt.Printf("%q\n", n.Data)
}
printText = printText || (n.Type == html.ElementNode && n.Data == "span")
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c, printText)
}
}
f(doc, false)