You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Notion API获取父页面中内嵌子数据库的ID

获取Notion页面内嵌子数据库ID的正确方法

你之前调用的GET /v1/pages接口只返回页面的元数据(比如标题、创建时间等),不会包含页面内的具体内容块。内嵌的子数据库属于页面的子块,得用Retrieve block children接口来获取。

具体操作步骤

  • 调用GET /v1/blocks/{page_id}/children接口,这里的page_id就是你之前使用的父页面ID(Notion里页面本身也是一个block)。
  • 遍历返回的子块列表,筛选出type为child_database的块,从中提取database_id。

修改后的Python代码

import requests, json

API_KEY = "secret_MyKey_:)"
# 替换为你的父页面ID
PAGE_ID = "parent_page_id_:)"
get_url = f"https://api.notion.com/v1/blocks/{PAGE_ID}/children"

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28",
}

response = requests.get(get_url, headers=headers)

if response.status_code == 200:
    data = response.json()
    # 遍历子块,收集内嵌数据库信息
    child_databases = []
    for block in data.get("results", []):
        if block["type"] == "child_database":
            db_info = {
                "title": block["child_database"]["title"],
                "database_id": block["child_database"]["database_id"]
            }
            child_databases.append(db_info)
    
    # 输出结果并保存
    print("找到的内嵌子数据库:")
    for db in child_databases:
        print(f"标题: {db['title']}, ID: {db['database_id']}")
    
    with open("child_databases.json", "w") as f:
        json.dump(child_databases, f, indent=2)
else:
    print(f"请求失败,状态码: {response.status_code}")

补充说明

  • 接口返回的results数组包含页面所有子块,除了子数据库,还可能包含文本、图片等其他类型的块。
  • 每个child_database类型的块中,child_database字段直接包含数据库的标题和ID,按需提取即可。

内容的提问来源于stack exchange,提问作者SenaThenu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:33:09