Skip to content

Instantly share code, notes, and snippets.

@kukat
Created April 3, 2022 15:59
Show Gist options
  • Save kukat/fe2ef3279dcf20b3ad3addb762e54358 to your computer and use it in GitHub Desktop.
Save kukat/fe2ef3279dcf20b3ad3addb762e54358 to your computer and use it in GitHub Desktop.
go spider with colly and gjson
package main
import (
"fmt"
"github.com/gocolly/colly/v2"
"github.com/tidwall/gjson"
)
func main() {
c := colly.NewCollector()
// Find and visit all links
c.OnResponse(func(r *colly.Response) {
fmt.Println("Visited", r.Request.URL)
fmt.Println("code: ", r.StatusCode)
body := string(r.Body)
fmt.Println("body: ", body)
json := gjson.Get(body, "json.array")
fmt.Println("array: ", json)
json.ForEach(func(key, value gjson.Result) bool {
fmt.Println("id: ", value.Get("id"))
return true
})
})
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Content-Type", "application/json;charset=UTF-8")
fmt.Println("Visiting", r.URL)
})
payload := []byte(`{"login":"my_login","password":"my_password", "array": [{"id": "1"}, {"id": "2"}]}`)
url := "https://httpbin.org/post"
c.PostRaw(url, payload)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment