-
-
Save vanleantking/4f0000f23fada55bbb845c13d85c7983 to your computer and use it in GitHub Desktop.
Goquery Remove Child Elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stackoverflow Answer | |
// Original Question: http://stackoverflow.com/questions/32635943/get-text-from-div-without-child-elements/32640258#32640258 | |
package main | |
import ( | |
"fmt" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
) | |
var ( | |
html = `<html><body><div class="outter-class"> | |
<h1 class="inner-class"> | |
The string I need | |
<span class="other-class" >Some value I don't need</span> | |
<span class="other-class2" title="sometitle"></span> | |
</h1> | |
<div class="other-class3"> | |
<h3>Some heading i don't need</h3> | |
</div> | |
</div></body></html>` | |
) | |
func main() { | |
r := strings.NewReader(html) | |
doc, err := goquery.NewDocumentFromReader(r) | |
if err != nil { | |
panic(err) | |
} | |
// h1 := doc.Find("h1") | |
// h1.Children().Remove() | |
// text := h1.Text() | |
text := doc.Find("h1").Children().Remove().End().Text() | |
text = strings.TrimSpace(text) | |
fmt.Println(text) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment