Library usage¶
This is usage with your Python applications.
Simple way¶
ogpy.client.fetch() is useful function to generate Opengraph dataset object from URL.
from pprint import pprint
from ogpy.client import fetch
data = fetch("https://ogp.me")
pprint(data)
Metadata(audio=None,
description='The Open Graph protocol enables any web page to become a '
'rich object in a social graph.',
determiner='',
locale='en_US',
locale_alternates=[],
site_name=None,
video=None,
title='Open Graph protocol',
type='website',
url='https://ogp.me/',
images=[ImageMetadata(url='https://ogp.me/logo.png',
secure_url=None,
type='image/png',
width=300,
height=300,
alt='The Open Graph logo')])
ogpy.types.Metadata is data class that has metadata of Opengraph from fetched content.
You access attribute of object to use in your works. (e.g. data.url )
Cache control¶
Almost contents response itself with Cache-Control header.
OGPy provides function to return expired date with data object.
from ogpy.client import fetch_for_cache
data, age = fetch_for_cache("https://ogp.me")
ogpy.client.fetch_for_cache() returns tuple of data object and cachable age.
You can store data object until expire age if you need.
Browser mode¶
There are cases that server does not response with metatags even if website has this actually. This main reason is due to block excluded browser using by human.
When you want to read metatags anywhere, you should try “Browser mode” library.
from pprint import pprint
from ogpy.client.browser import fetch # Instead of ``from ogpy.client``.
data = fetch("https://ogp.me")
pprint(data)
This code displays message as same as run.py.
More information¶
See API reference.