使用python代码更新/删除汇合页面

Ris*_*shi 5 python python-2.7 confluence-rest-api

我想通过Confluence REST API更新一个Confluence页面。请建议一个代码片段,我可以通过它的“页面标题”更新页面。

假设我的 Confluence 站点是https://wiki.mydomain.com,页面标题是TEST,空间是TST

bud*_*mat 12

您还可以使用Atlassian Python APIConfluence 模块从 Python 更新/删除 Confluence 页面。这基本上是 REST API 的包装器,用于提供简单的接口。

安装通过pip

pip install atlassian-python-api
Run Code Online (Sandbox Code Playgroud)

用法:

from atlassian import Confluence

conf_site = 'https://wiki.mydomain.com'
conf_user = 'username'
conf_pass = 'password'

page_title = 'TEST' 
page_space = 'TST'

# connect to Confluence 
conf = Confluence(url=conf_site, username=conf_user, password=conf_pass)

# resolve page ID
page_id = conf.get_page_id(page_space, page_title)

# optonal: get current page content, in case you want to base your editing on that
page = conf.get_page_by_id(page_id, expand='body.storage')
page_content = page['body']['storage']['value']

# new page content
page_content =  '<p>This is the new updated text of the page</p>'

# update page with new content
conf.update_page(page_id, page_title, page_content)
Run Code Online (Sandbox Code Playgroud)

或者,删除:

# This method removes a page, if it has recursive flag, method removes including child pages
conf.remove_page(page_id, status=None, recursive=False)
Run Code Online (Sandbox Code Playgroud)


Sal*_*rsa 2

正如您在 Atlassian 文档(此处)中看到的,您可以通过以下方式更新页面curl

curl -u admin:admin -X PUT -H 'Content-Type: application/json' -d'{"id":"3604482","type":"page",
"title":"new page","space":{"key":"TST"},"body":{"storage":{"value":
"<p>This is the updated text for the new page</p>","representation":"storage"}},
"version":{"number":2}}' http://localhost:8080/confluence/rest/api/content/3604482 | python -mjson.tool
Run Code Online (Sandbox Code Playgroud)

不过,它适用于页面 ID,而不是页面标题。您可以通过以下方式获取 id:

curl -u admin:admin -X GET "http://localhost:8080/confluence/rest/api/content?title=myPage%20Title
&spaceKey=TST&expand=history" | python -mjson.tool
Run Code Online (Sandbox Code Playgroud)

顺便说一句,由于您看起来像新用户,这里我们不会提供代码片段,您需要告诉我们您尝试过什么以及您实际遇到的问题是什么。我建议您也看看如何提出一个好问题:-)