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)
}
You're trying to filter a selection that is empty.
From what I see you're trying to do in your question, you could simply replace the
FilterwithFind. So in your case it would be:Once you have a selection containing elements, then you can use
Filter. So, for example, to get the second article, you could do :To read more about
Filter, check out these resources:FilterdocumentationFinddocumentationPS: You can also combine it in single selector toFindspecific article