Goquery选择器 meta[property=og:image]。

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

Goquery Select meta[property=og:image]?

问题

Goquery 从语法上讲,它尽可能接近于jQuery,使用相同的函数名称,并具有温暖而令人愉悦的可链接接口。

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

显然,它与那个j-thing还不够接近。

如何使用goquery从网页中获取og数据?

英文:

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?

答案1

得分: 6

刚刚找到了解决方法 - 希望这对其他人有所帮助

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

})
英文:

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

})

答案2

得分: 1

我正在为您翻译以下内容:

我一直在寻找这个,然后我找到了另一种方法。

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

英文:

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

huangapple
  • 本文由 发表于 2015年5月4日 14:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/30023633.html
匿名

发表评论

匿名网友

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

确定