英文:
goquery filter doesn't return expected result
问题
神奇的互联网,
我正在尝试使用"github.com/PuerkitoBio/goquery"从HTML字符串中过滤元素。不幸的是,过滤函数没有返回预期的结果。我本来期望得到所有文章的列表,但实际上什么都没有返回。
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)
}
请帮我翻译以上内容。
英文:
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)
}
答案1
得分: 2
你正在尝试过滤一个空的选择。
根据我在你的问题中看到的,你可以简单地将Filter
替换为Find
。所以在你的情况下,可以这样写:
articles := qHtml.Find("article")
一旦你有了包含元素的选择,就可以使用Filter
。例如,要获取第二篇文章,可以这样做:
articles := qHtml.Find("article").Filter(":nth-child(2)")
要了解更多关于Filter
的信息,请参考以下资源:
PS
:你还可以将它组合成单个选择器来Find
特定的文章
articles := qHtml.Find("article:nth-child(2)")
英文:
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 Filter
with Find
. So in your case it would be:
articles := qHtml.Find("article")
Once you have a selection containing elements, then you can use Filter
. So, for example, to get the second article, you could do :
articles := qHtml.Find("article").Filter(":nth-child(2)")
To read more about Filter
, check out these resources:
PS
: You can also combine it in single selector to Find
specific article
articles := qHtml.Find("article:nth-child(2)")
答案2
得分: 0
根据doc
:
> Filter
将匹配的元素集合减少为与选择器字符串匹配的元素。它返回一个新的Selection对象,用于匹配元素的子集。
由于在Filter
之前,您没有匹配任何内容,所以您得到了空结果,因为Filter
没有匹配的元素。
在应用Filter
之前,您必须先匹配元素或获取Selection
节点集。
为了解决这个问题,您可以使用Find
或FindMatcher
代替Filter
:
searchStr := `article`
articles := qHtml.Find(searchStr)
articles := qHtml.FindMatcher(goquery.Single(searchStr))
英文:
As per doc
> Filter
reduces the set of matched elements to those that match the selector string. It returns a new Selection object for this subset of matching elements.
Since before Filter
you are not matching anything that's why you got empty result since Filter
got no matched elements.
You have to first match elements or get Selection
set of nodes before you can apply Filter
To solve the issue instead of Filter
you can use Find
or FindMatcher
searchStr := `article`
articles := qHtml.Find(searchStr)
articles := qHtml.FindMatcher(goquery.Single(searchStr))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论