magical internet,
I am trying to filter elements from an html string using "github.com/PuerkitoBio/goquery". Unfortunately, the filter function does not return the expected result. I would have expected to get back a list of all articles but instead, ... nothing.
package main
import (
"fmt"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
var html = `
<section>
<article>
<h1>Article 1</h1>
<p>Text for article #1</p>
</article>
<article>
<h1>Article 2</h1>
<p>Text for article #2</p>
</article>
</section>
`
func main() {
qHtml, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
panic(err)
}
articles := qHtml.Filter(`article`)
fmt.Println(articles.Nodes)
goquery.Render(os.Stdout, articles)
}
As per
docSince before
Filteryou are not matching anything that's why you got empty result sinceFiltergot no matched elements.You have to first match elements or get
Selectionset of nodes before you can applyFilterTo solve the issue instead of
Filteryou can useFindorFindMatcher