How to fetch meta properties (images, videos, titles..) from links like Facebook does?
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 nam开发者_如何学JAVAe="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!
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
It's called link scraping. You'll need to use both client-side js and server-side ruby.
Process looks like this:
- Your client calls scraper function on your server via ajax
- Scraper function fetches the url and inspects the relevant meta
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
精彩评论