'Goquery Select meta[property=og:image]?

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?



Solution 1:[1]

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)
    }

})

Solution 2:[2]

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)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris
Solution 2 twiny