1

Goquery Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface.

doc.Find("meta[property='og:image']").Each(func(i int, s *goquery.Selection) {
    fmt.Fprintln("og data=", s)
})

Apparently not close enough to that j-thing.

How can you get the og data in a webpage from goquery?

Community
  • 1
  • 1
Chris
  • 18,075
  • 15
  • 59
  • 77

2 Answers2

6

Just figured it out - hope this helps someone else out there

doc.Find("meta").Each(func(i int, s *goquery.Selection) {
    op, _ := s.Attr("property")
    con, _ := s.Attr("content")
    if op == "og:image" {
        fmt.Fprintln("og data=", con)
    }

})
Chris
  • 18,075
  • 15
  • 59
  • 77
1

I was looking for this and I found an other way.

package main

import (
    "fmt"
    "net/http"

    "github.com/PuerkitoBio/goquery"
)

func main() {
    baseURL := `REPLACE_WITH_URL`

    resp, err := http.Get(baseURL)
    if err != nil {
        fmt.Println(err)
        return
    }

    doc, err := goquery.NewDocumentFromResponse(resp)
    if err != nil {
        fmt.Println(err)
        return
    }

    imgURL, found := doc.Find(`meta[property="og:image"]`).Attr("content")

    fmt.Println(imgURL, found)
}

twiny
  • 284
  • 5
  • 11