Confluence API. How get page content with Python?
Asked by Oleg Gonshtein:

Hi,

I need to get the table from page in confluence and use it in Jupyter Notebook. I read the documentation <https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/> and tried using examples.

But when I run request

params = (  
('expand', 'body.storage'),  
)  
response = requests.get('<https://wiki.tele2.ru/pages/rest/api/content/27691117>', params=params, auth=('login', 'pass'))

I get empty response.

Can anyone help me?


Answers:
author: Jerome Diard
body: You may use the atlassian-python-api module from <https://atlassian-python-api.readthedocs.io/confluence.html>  

use the confluence.get_page_by_id API (warning the html document has a typo for this API, it has no "self" parameter), make sure to set the expand parameter to "body.storage"

<br />

The API then returns d, a dictionary where the page view storage content is located in d\['body'\]\['storage'\]\['value'\]

comments: author: Steve Suranie
body: [@Jerome Diard](/t5/user/viewprofilepage/user-id/4888587) Thanks for pointing out the typo, was going crazy for a bit. : )


author: Hugh
body: <https://gist.github.com/quest4i/3b8b287e8158b63220550ecbcb98555f>

covert confluece page to markdown


author: m350funt
body: [@Jerome Diard](/t5/user/viewprofilepage/user-id/4888587) was close but not right  

here is code that works for you, if you want get body of page in confluence by python.  

\`\`\`  
from atlassian import Confluence

confluence = Confluence(  
url='<https://my.site>',  
username = confluence_login,  
password = confluence_password,  
verify_ssl = False  
)  
page_html = confluence.get_page_by_id(page_id, "space,body.view,version,container")  
print(page_html\['body'\]\['view'\]\['value'\]  
)

\`\`\`

comments: author: Karthik PANICKER
body: I don't have a direct confluence username and password, what would I need to do to do the same using my microsoft credentials


author: Thiago Masutti
body: Hi [@Oleg Gonshtein](/t5/user/viewprofilepage/user-id/4054647)

Welcome to the Atlassian Community :)

I'm not the best with Python coding, but I believe I can help you.

Since you are using the [requests library](https://requests.readthedocs.io/en/master/), I'm assuming you already installed it [using pip](https://packaging.python.org/tutorials/installing-packages/).

The below script should get you the page contents.

```
import requests

contentApiUrl = '/rest/api/content'
# Change these based on your instance
confluenceBaseUrl = 'http://localhost:6740/c740'
pageId = '3309573'
username = 'admin'
password = 'admin'

requestUrl = '{confluenceBaseUrl}{contentApiUrl}/{pageId}?expand=body.storage'.format(confluenceBaseUrl = confluenceBaseUrl, contentApiUrl = contentApiUrl, pageId = pageId)
print requestUrl

requestResponse = requests.get(requestUrl, auth=(username, password))

print requestResponse.json()
```

A search for other Community posts will certainly find some more elaborated Python scripts, such as in [this post](https://community.atlassian.com/t5/Confluence-questions/How-to-update-a-page-with-Python-using-REST-API/qaq-p/480627).

I hope that helps.

Kind regards,  
Thiago Masutti


Tags: atl-gruland

confluence-server

server