GreenRaccoon23
GreenRaccoon23

Reputation: 3833

Iterate over *goquery.Selection

I just started learning Go a couple days ago, so bear with me please. :)

I'm fetching text from a web page with goquery. Like this:

package main

import (
    "fmt"
    "log"
    "github.com/PuerkitoBio/goquery"
)

func ExampleScrape() {
    doc, err := goquery.NewDocument("http://lifehacker.com")
    if err != nil {
        log.Fatal(err)
        fmt.Println("fail")
    } else {
        fmt.Println("got it")
    }

    h1_text := doc.Find("h1").Text()
    fmt.Println(h1_text)
}

func main() {
    ExampleScrape()
}

This works great. What I can't figure out is how to turn the doc.Find("h1").Text() selection into an array or slice so that I can iterate over them (or, even better, figuring out if goquery has a function for this). I'm sure there's a way to do this, right?

I tried doing this (inside func ExampleScrape):

var x []string
doc.Find("h1").Each(func(i int, s *goquery.Selection) {
    append(x, s.Text())
})

but it didn't work because append in the 'nested'/closure function remains local to that function--it doesn't get returned back to func ExampleScrape. So then I tried this:

x := doc.Find("h1").Each(func(i int, s *goquery.Selection) {
    return s.Text()
})
for _, i := range x {
    fmt.Println(x)
}

but *goquery.Selection types can't be ranged over.

Is there a way to iterate over *goquery.Selection's like this?

You guys on here are awesome, by the way. I'm always blown away by the answers I get on here. If someone can explain how to do this, thanks a googolplex in advance. :)

Upvotes: 0

Views: 2901

Answers (1)

Dean Elbaz
Dean Elbaz

Reputation: 2450

I think your first attempt could work if you used append properly. append(x, s.Text()) does not change x, rather it returns a new slice.

so you really need to do:

x = append(x, s.Text())

Upvotes: 3

Related Questions