88dax
88dax

Reputation: 307

How to get content/preview of an URL in Django?

For my web app, I need to get a preview of URL given by the user. For example, in Facebook, if the user copy-paste an URL,it automatically fetches the content of the URL and displays a preview of the page. How does it work? Is it a feature of Ajax? I need to do it in Django. Any tutorials or demos?

Upvotes: 4

Views: 4410

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

For Facebook, there is a standard way to make your site "preview" compatible, using open graph tags. The most common ones are:

<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />

The complete list is available at the open graph protocol site. You can use PyOpenGraph to parse a URL for its open graph tags. Here is the example from the readme:

>>> import PyOpenGraph
>>> og = PyOpenGraph('http://www.rottentomatoes.com/m/10011268-oceans/')
>>> print og.metadata
{'url': 'http://www.rottentomatoes.com/m/10011268-oceans/',
 'site_name': 'Rotten Tomatoes',
 'image': 'http://images.rottentomatoes.com/images/movie/custom/68/10011268.jpg',
 'type': 'movie',
 'title': 'Oceans'}
>>> print og.metadata['title']
Oceans

Upvotes: 6

Related Questions