如何像 Facebook 那样从链接中获取元属性(图像、视频、标题......)?
我正在尝试为网络应用程序添加一项功能,当用户发布链接时,会以某种方式获取标题图片 URL,以便它可以显示在我的网络应用程序上。
Facebook 已经这样做了;当您共享链接时,它会获取此信息并显示标题、简介、图片等。
我知道它会这样标识相关数据:
<meta name="title" content="title" />
<meta name="description" content="description " />
<link rel="image_src" href="thumbnail_image" />
但是我如何才能真正获取这些字符串呢?
我正在使用 Ruby on Rails 进行编程,因此与此兼容的答案,或者可能使用 javascript,将是最有用的!
I'm trying to add a functionality for a webapp where when users post links, the title picture URL is fetched somehow, so that it can be displayed on my web app.
Facebook does this already; when you share a link it fetches this information and displays the title, a blurb, a picture etc.
I know that it identifies the pertinent data as such:
<meta name="title" content="title" />
<meta name="description" content="description " />
<link rel="image_src" href="thumbnail_image" />
But how could I it actually fetch these strings?
I'm programming in Ruby on Rails, so an answer compatible with that, or using javascript perhaps, would be most useful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个 gem 封装了 OpenGraph,这就是 Facebook 使用的。好像已经闲置一年左右了。
https://github.com/intridea/opengraph
There's a gem that wraps OpenGraph, which is what Facebook uses. Seems its been idle for a year or so.
https://github.com/intridea/opengraph
这称为链接抓取。您需要使用客户端 js 和服务器端 ruby。
流程如下所示:
It's called link scraping. You'll need to use both client-side js and server-side ruby.
Process looks like this:
这可能有点晚了,但我建议查看一个名为 nokogiri 的 gem,它可以让您使用 css 选择器或 xpath 从其他网页抓取数据。如果您想了解它的功能,可以查看随附的 Railscast。 http://railscasts.com/episodes/190-screen-scraping-with-nokogiri
This may be a little late but I would suggest looking at a gem called nokogiri, it will let you scrape data from other web pages using css selectors or xpath. If you want to see what it can do, there is an accompanying railscast about it. http://railscasts.com/episodes/190-screen-scraping-with-nokogiri