2

我有一个购物推荐网站,它允许用户发布他们推荐的商品的网址。

该表单目前只包含一个输入到主产品页面的 url。

如果用户要发布如下链接:

http://www.homedepot.com/webapp/wcs/stores/servlet/ProductDisplay?productId=203533553&storeId=10051&langId=-1&catalogId=10053&cm_sp=BlackFriday-_-11_23-_-Hero_C-_-349_Each_GE_Washer_Dryer_Special_Buy#.UK-DHuOe_VQ

我想显示这个 url 中包含的 og:image 的图像标签。

使用 facebook 开放图形协议页面,我看到上面的示例 url 确实有一个 og:image

http://developers.facebook.com/tools/debug/og/object?q=http%3A%2F%2Fwww.homedepot.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FproductId%3D203533553%26storeId%3D10051% 26langId%3D-1%26catalogId%3D10053%26cm_sp%3DBlackFriday-_-11_23-_-Hero_C-_-349_Each_GE_Washer_Dryer_Special_Buy%23.UK-DHuOe_VQ

如何将 url 提取到 og:image?

4

4 回答 4

6

I ended up using Nokogiri like below, while relying heavy on this tutorial by Railscasts.

def photo_from_url(url)
  if !Nokogiri::HTML(open(url)).css("meta[property='og:image']").blank?
    photo_url = Nokogiri::HTML(open(url)).css("meta[property='og:image']").first.attributes["content"]
    self.photo = URI.parse(photo_url)
    self.save
  end
end

I found this to be more reliable than the opengraph gem:

url_with_photo = opengraph.fetch(url)
url_with_photo.image

as this second method sometimes doesn't work, even on pages that have an og:image tag in the source code. (I didn't implement it completely wrong, as it did work on some pages...).

于 2012-11-26T23:15:07.167 回答
0

如果您知道页面的 ID,则可以将 /picture 附加到 Graph API URL 的末尾,以重定向到与其关联的图像,无论是显式还是隐式。

例如,下面的一位评论者提到了 ID 10150708135007395 (http://www.devils-heaven.com/heroku-gratis-ssl-fur-facebook-apps/),所以要获取缩略图,只需抓取:

https://graph.facebook.com/10150708135007395/picture
于 2012-11-27T02:00:50.423 回答
0

您的网页没有任何 Open Graph 标签。有关说明和示例,请参见此处:http: //ogp.me/

当用户分享链接时,Facebook 会读取这些标签。

调试器告诉你怎么做顺便说一句:

推断属性:应显式提供“og:url”属性,即使可以从其他标签推断出值。

推断属性:应显式提供“og:title”属性,即使可以从其他标签推断出值。

推断属性:应显式提供“og:description”属性,即使可以从其他标签推断出值。

推断属性:应显式提供“og:image”属性,即使可以从其他标签推断出值。

但即使没有(非常重要和有用的)标签,我也可以在 facebook 上分享您的链接,并且效果很好。

于 2012-11-23T14:27:03.670 回答
0
module MetaParser

  require 'open-uri'

  COMMON_USER_AGENTS = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36','Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36']  

  def self.parse_doc(url)
    Nokogiri::HTML.parse(open(url,'User-Agent' => COMMON_USER_AGENTS.sample ))  
  end   

  module Facebook
    def self.get_attributes(url)
      attributes = {}
      doc = MetaParser.parse_doc(url)

      attributes[:url]   = url
      attributes[:site_name] = doc.at('meta[property="og:site_name"]')['content']
      attributes[:title] = doc.at('meta[property="og:title"]')['content'] 
      attributes[:description] = doc.at('meta[property="og:description"]')['content']
      attributes[:image] = doc.at('meta[property="og:image"]')['content']

      return attributes
    end
  end # Facebook  

  module Twitter
    def self.get_attributes(url)
      attributes = {}
      doc = MetaParser.parse_doc(url)

      attributes[:url]   = url
      attributes[:site_name] = doc.at('meta[name="twitter:site"]')['content']
      attributes[:title] = doc.at('meta[name="twitter:title"]')['content'] 
      attributes[:description] = doc.at('meta[name="twitter:description"]')['content']
      attributes[:image] = doc.at('meta[name="twitter:image"]')['content']

      return attributes
    end  
  end # Twitter     

end

用法:

MetaParser::Facebook.get_attributes("google.com") MetaParser::Twitter.get_attributes("google.com")

于 2017-08-16T17:09:20.900 回答