Google Knowledge Graph panels appear at the top of search results for entities like brands, people, places, and products. They contain structured information including descriptions, official website links, social profiles, and key attributes. The Scavio API includes the knowledge_graph field in its SERP response when a panel is available. This tutorial shows how to query for an entity, extract the knowledge_graph object, and use it to build a structured entity profile without screen-scraping.
Prerequisites
- Python 3.8 or higher
- requests library installed
- A Scavio API key
- Basic understanding of JSON parsing
Walkthrough
Step 1: Query for an entity that has a Knowledge Panel
Knowledge Panels appear for well-known entities. Search for a brand, person, or place by name. Google surfaces a panel when confident about the entity.
def get_serp(entity: str) -> dict:
response = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={"x-api-key": API_KEY},
json={"query": entity, "country_code": "us"}
)
response.raise_for_status()
return response.json()Step 2: Extract the knowledge_graph field
Check for the knowledge_graph key in the response. It will be None if Google does not show a panel for the query.
data = get_serp("OpenAI")
kg = data.get("knowledge_graph")
if kg:
print(kg)
else:
print("No knowledge graph found for this query")Step 3: Parse entity attributes
The knowledge_graph object contains fields like title, type, description, website, and a list of attributes. Extract them by key.
if kg:
print("Title:", kg.get("title"))
print("Type:", kg.get("type"))
print("Description:", kg.get("description"))
print("Website:", kg.get("website"))
for attr in kg.get("attributes", []):
print(f" {attr['name']}: {attr['value']}")Step 4: Build a structured entity profile
Combine the knowledge graph fields into a dictionary that can be stored or fed into a downstream pipeline.
def build_profile(kg: dict) -> dict:
return {
"name": kg.get("title"),
"entity_type": kg.get("type"),
"description": kg.get("description"),
"website": kg.get("website"),
"attributes": {a["name"]: a["value"] for a in kg.get("attributes", [])},
}Python Example
import os
import requests
API_KEY = os.environ.get("SCAVIO_API_KEY", "your_scavio_api_key")
ENDPOINT = "https://api.scavio.dev/api/v1/search"
def get_knowledge_graph(entity: str) -> dict | None:
r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
json={"query": entity, "country_code": "us"})
r.raise_for_status()
return r.json().get("knowledge_graph")
def build_profile(kg: dict) -> dict:
return {
"name": kg.get("title"),
"type": kg.get("type"),
"description": kg.get("description"),
"website": kg.get("website"),
"attributes": {a["name"]: a["value"] for a in kg.get("attributes", [])},
}
if __name__ == "__main__":
entities = ["OpenAI", "Stripe", "Cloudflare"]
for name in entities:
kg = get_knowledge_graph(name)
if kg:
profile = build_profile(kg)
print(profile)
else:
print(f"No KG for {name}")JavaScript Example
const API_KEY = process.env.SCAVIO_API_KEY || "your_scavio_api_key";
const ENDPOINT = "https://api.scavio.dev/api/v1/search";
async function getKnowledgeGraph(entity) {
const res = await fetch(ENDPOINT, {
method: "POST",
headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ query: entity, country_code: "us" })
});
const data = await res.json();
return data.knowledge_graph || null;
}
function buildProfile(kg) {
return {
name: kg.title,
type: kg.type,
description: kg.description,
website: kg.website,
attributes: Object.fromEntries((kg.attributes || []).map(a => [a.name, a.value]))
};
}
async function main() {
const kg = await getKnowledgeGraph("OpenAI");
if (kg) console.log(JSON.stringify(buildProfile(kg), null, 2));
}
main().catch(console.error);Expected Output
{
"knowledge_graph": {
"title": "OpenAI",
"type": "Research laboratory",
"description": "American artificial intelligence research laboratory...",
"website": "https://openai.com",
"founded": "December 11, 2015",
"headquarters": "San Francisco, California",
"attributes": [
{ "name": "Founded", "value": "December 11, 2015" },
{ "name": "CEO", "value": "Sam Altman" }
]
}
}