goquery过滤器没有返回预期的结果。

huangapple go评论85阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strings&quot;

	&quot;github.com/PuerkitoBio/goquery&quot;
)

var html = `
&lt;section&gt;
	&lt;article&gt;
		&lt;h1&gt;Article 1&lt;/h1&gt;
		&lt;p&gt;Text for article #1&lt;/p&gt;
	&lt;/article&gt;
	&lt;article&gt;
		&lt;h1&gt;Article 2&lt;/h1&gt;
		&lt;p&gt;Text for article #2&lt;/p&gt;
	&lt;/article&gt;
&lt;/section&gt;
`

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(&quot;article&quot;)

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(&quot;article&quot;).Filter(&quot;:nth-child(2)&quot;)

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(&quot;article:nth-child(2)&quot;)

答案2

得分: 0

根据doc

> Filter将匹配的元素集合减少为与选择器字符串匹配的元素。它返回一个新的Selection对象,用于匹配元素的子集。

由于在Filter之前,您没有匹配任何内容,所以您得到了空结果,因为Filter没有匹配的元素。

在应用Filter之前,您必须先匹配元素或获取Selection节点集。

为了解决这个问题,您可以使用FindFindMatcher代替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))

huangapple
  • 本文由 发表于 2022年5月11日 02:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/72191242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定