OpenGraph.io

Python Open Graph Example

Python is the go-to language for web scraping, data extraction, and AI/ML workflows. This example shows how to use OpenGraph.io to pull metadata from any URL with just a few lines of code. You get back structured data including the title, description, and images - exactly what you need for building link previews, populating content databases, or feeding data into LLM pipelines.

Open Graph Results

Basic Example with Requests

The simplest way to call the API. Just URL-encode your target site, make a GET request, and parse the JSON response. The hybridGraph object contains the most reliable data - it combines OpenGraph tags with HTML-inferred content for sites that don't have proper OG tags.

import requests

# Replace with your actual app_id from dashboard.opengraph.io
app_id = 'YOUR_APP_ID'
target_url = 'https://github.com'

# URL encode the target and make the request
encoded_url = requests.utils.quote(target_url, safe='')
api_url = f'https://opengraph.io/api/1.1/site/{encoded_url}'

response = requests.get(api_url, params={'app_id': app_id})

if response.status_code == 200:
    data = response.json()
    hybrid = data.get('hybridGraph', {})
    
    print(f"Title: {hybrid.get('title')}")
    print(f"Description: {hybrid.get('description')}")
    print(f"Image: {hybrid.get('image')}")
else:
    print(f"Error: {response.status_code}")

Async Example for Batch Processing

Need to process a bunch of URLs at once? Here's an async version using aiohttp. This is way faster than sequential requests when you're building a content aggregator or processing URLs for a RAG pipeline.

import aiohttp
import asyncio

async def get_opengraph_data(url: str, app_id: str) -> dict:
    """Fetch OpenGraph data asynchronously - useful for batch processing"""
    import urllib.parse
    encoded = urllib.parse.quote(url, safe='')
    api_url = f'https://opengraph.io/api/1.1/site/{encoded}?app_id={app_id}'
    
    async with aiohttp.ClientSession() as session:
        async with session.get(api_url) as response:
            return await response.json()

# Example: fetch multiple URLs concurrently
async def main():
    app_id = 'YOUR_APP_ID'
    urls = [
        'https://github.com',
        'https://twitter.com', 
        'https://linkedin.com'
    ]
    
    tasks = [get_opengraph_data(url, app_id) for url in urls]
    results = await asyncio.gather(*tasks)
    
    for url, result in zip(urls, results):
        title = result.get('hybridGraph', {}).get('title', 'N/A')
        print(f"{url}: {title}")

asyncio.run(main())

When to Use This

This approach works well for: building Slack-style link previews in your app, populating a content database with metadata, pre-processing URLs for LLM context, or any situation where you need structured data about a webpage without scraping it yourself. The API handles JavaScript rendering, proxy rotation, and all the annoying edge cases so you don't have to.

check

First, make sure you have Python 3 installed. Then install the requests library

pip install requests

check

Save the code above to a file (e.g., opengraph_example.py) and run it

python opengraph_example.py