创建一个特定类型的数组并重复使用它。

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

Create an array of a certain type and reuse it

问题

我遇到了创建GraphQL解析器负载的问题。你可以如何重写代码以返回一个完整的数组?

你被困在c.OnHTML("article", func(e *colly.HTMLElement) {}中,无法在其外部返回数据。

type Article struct {
	Title     string `bson:"title"`
	Source    string `bson:"source"`
	Url       string `bson:"url"`
	Timestamp string `bson:"timestamp"`
}
func (r *RootResolver) News() ([]models.Article, error) {

	c := colly.NewCollector(
		colly.MaxDepth(2),
		colly.Async(),
	)

	c.Limit(&colly.LimitRule{Parallelism: 10})

	articles := []models.Article{}

	c.OnHTML("article", func(e *colly.HTMLElement) {
		articleModel := []models.Article{
			{
				Title:     e.ChildText("h3 a"),
				Source:    e.ChildText("a[data-n-tid]"),
				Timestamp: e.ChildAttr("div:last-child time", "datetime"),
				Url:       e.ChildAttr("a[href]", "href"),
			},
		}

		fmt.Println(articleModel)
	})

	c.Visit(SOMEURLHERE)
	c.Wait()

	return articles, nil
}
英文:

I'm having trouble creating the payload for my graphql resolver. How could I rewrite this to return a completed array?

I'm stuck inside of c.OnHTML("article", func(e *colly.HTMLElement) {} and can't return the data outside of it.

type Article struct {
	Title     string `bson:"title"`
	Source    string `bson:"source"`
	Url       string `bson:"url"`
	Timestamp string `bson:"timestamp"`
}
func (r *RootResolver) News() ([]models.Article, error) {

	c := colly.NewCollector(
		colly.MaxDepth(2),
		colly.Async(),
	)

	c.Limit(&colly.LimitRule{Parallelism: 10})

	articles := []models.Article{}

	c.OnHTML("article", func(e *colly.HTMLElement) {
		articleModel := []models.Article{
			{
				Title:     e.ChildText("h3 a"),
				Source:    e.ChildText("a[data-n-tid]"),
				Timestamp: e.ChildAttr("div:last-child time", "datetime"),
				Url:       e.ChildAttr("a[href]", "href"),
			},
		}

		fmt.Println(articleModel)
	})

	c.Visit(SOMEURLHERE)
	c.Wait()

	return articles, nil
}

答案1

得分: 0

如果你需要获取完整的文章列表,你需要在c.OnHTML函数内部将新的文章追加到articles切片中,而不是创建一个新的切片。

然后在News()方法的末尾返回articles切片。

    articles = append(articles, models.Article{
		Title:     e.ChildText("h3 a"),
		Source:    e.ChildText("a[data-n-tid]"),
		Timestamp: e.ChildAttr("div:last-child time", "datetime"),
		Url:       e.ChildAttr("a[href]", "href"),
	})

在你当前的实现中,articles := []models.Article{}创建了一个空的models.Article类型的切片。而在c.OnHTML内部,你又创建了另一个名为articleModelmodels.Article类型的切片,并添加了一个元素。这两个切片之间没有任何连接。所以你需要将新的元素追加到articles切片中,并返回它。

英文:

If you need to get full list of articles, you need to append new article into articles slice inside of your c.OnHTML function instead creating new slice.

and return articles slice end of the News() Method.

    articles = append(articles, models.Article{
		Title:     e.ChildText("h3 a"),
		Source:    e.ChildText("a[data-n-tid]"),
		Timestamp: e.ChildAttr("div:last-child time", "datetime"),
		Url:       e.ChildAttr("a[href]", "href"),
	})

In your current implementation, articles := []models.Article{} creates an empty slice of models.Article type. And inside c.OnHTML, you creating another slice of models.Article type named articleModel with and element. That two slices has no any connection. So you need to append new element to your articles slice and return it with articles.

huangapple
  • 本文由 发表于 2021年8月29日 10:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68969462.html
匿名

发表评论

匿名网友

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

确定