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
doc
Since before
Filter
you are not matching anything that's why you got empty result sinceFilter
got no matched elements.You have to first match elements or get
Selection
set of nodes before you can applyFilter
To solve the issue instead of
Filter
you can useFind
orFindMatcher