Google 知识图面板出现在品牌、人物、地点和产品等实体的搜索结果顶部。它们包含结构化信息,包括描述、官方网站链接、社交资料和关键属性。当面板可用时,Scadio API 在其 SERP 响应中包含 Knowledge_graph 字段。本教程展示如何查询实体、提取knowledge_graph对象并使用它构建结构化实体配置文件而无需屏幕抓取。
前置条件
- Python 3.8 或更高版本
- 请求已安装库
- Scavio API 密钥
- JSON解析的基本了解
操作指南
步骤 1: 查询具有知识面板的实体
知识面板显示知名实体。按名称搜索品牌、人物或地点。当谷歌对该实体有信心时,就会出现一个小组。
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()步骤 2: 提取knowledge_graph字段
检查响应中的knowledge_graph 键。如果 Google 不显示查询面板,则该值将为 None。
data = get_serp("OpenAI")
kg = data.get("knowledge_graph")
if kg:
print(kg)
else:
print("No knowledge graph found for this query")步骤 3: 解析实体属性
Knowledge_graph 对象包含标题、类型、描述、网站和属性列表等字段。通过密钥提取它们。
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']}")步骤 4: 构建结构化实体档案
将知识图字段组合成可以存储或馈送到下游管道的字典。
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 示例
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 示例
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);预期输出
{
"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" }
]
}
}