激活词: Notion / 笔记管理 / 知识库
bash
pip install notion-client
python
from notion_client import AsyncClient
import asyncio
class NotionHelper:
def __init__(self, token: str):
self.client = AsyncClient(auth=token)
async def create_page(self, parent_id: str, title: str, content: str = ""):
await self.client.pages.create(
parent={"database_id": parent_id},
properties={
"title": {
"title": [{"text": {"content": title}}]
}
},
children=[{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": content}}]
}
}]
)
async def query_database(self, db_id: str, filter_props: dict = None):
params = {"database_id": db_id}
if filter_props:
params["filter"] = filter_props
return await self.client.databases.query(**params)
async def update_page(self, page_id: str, properties: dict):
await self.client.pages.update(page_id, properties=properties)
async def get_page(self, page_id: str):
return await self.client.pages.retrieve(page_id)
async def search(self, query: str):
return await self.client.search(query)
python
async def main():
helper = NotionHelper("secret_xxx")
# 创建页面
await helper.create_page(
parent_id="database_id",
title="AI学习笔记",
content="今天学习了机器学习..."
)
# 搜索页面
results = await helper.search("AI")
for page in results['results']:
print(page['title'])
# 查询数据库
pages = await helper.query_database("database_id")
for page in pages['results']:
print(page['properties']['title']['title'][0]['text']['content'])
asyncio.run(main())
类型说明title标题rich_text文本number数字checkbox复选框select单选multi_select多选date日期url链接email邮箱phone_number电话