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

如何通过API从Confluence页面获取表格内容并输出为JSON格式

核心问题解答

1. atlassian库是否支持直接导出表格为JSON?

atlassian的Python Confluence组件没有内置表格转JSON的能力,返回的页面内容固定为Confluence原生存储格式的HTML,需要自行解析HTML提取表格内容后再序列化为JSON。

2. requests请求返回404的排查方案

  • 你代码中写死的page_id = "12345"大概率和通过get_page_by_title查询到的实际页面ID不符,可以先打印print(page["id"])获取真实页面ID替换后重试
  • 确认当前API密钥对应的账号有目标页面的查看权限

表格转JSON实现方案

先安装解析依赖:

pip install beautifulsoup4

完整实现代码:

from atlassian import Confluence
import os
import json
from bs4 import BeautifulSoup

user = "me@myself.com"
api_key = os.environ['confluence_api_key']
server = "https://xxxxxx.atlassian.net"

# 初始化Confluence客户端
confluence = Confluence(url=server, username=user, password=api_key)
# 获取页面内容
page = confluence.get_page_by_title("TEST", "page 1", expand="body.storage")
html_content = page["body"]["storage"]["value"]

# 解析HTML提取所有表格
soup = BeautifulSoup(html_content, "html.parser")
tables = soup.find_all("table")
all_table_data = []

for table in tables:
    # 提取表头
    headers = [th.get_text(strip=True) for th in table.find_all("th")]
    table_rows = []
    # 提取行数据
    for tr in table.find_all("tr"):
        cell_values = [td.get_text(strip=True) for td in tr.find_all("td")]
        if cell_values:
            # 表头和单元格值映射为字典
            table_rows.append(dict(zip(headers, cell_values)))
    all_table_data.append(table_rows)

# 输出JSON格式结果
print(json.dumps(all_table_data, ensure_ascii=False, indent=2))

用你给出的测试页面内容运行上述代码,输出结果如下:

[
  [
    {
      "name": "text1",
      "type": "varchar(10)",
      "comment": ""
    },
    {
      "name": "123",
      "type": "int",
      "comment": ""
    }
  ]
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 15:45:04