찬찬의 Notion API 정복기 04 - properties에서 title 가져오기

소찬 (Chan)·2022년 8월 1일
1
post-thumbnail

이제 properties 구조에 대해 이해하였으니 title 을 가져올 차례입니다.
pages에 결과 정보를 집어 넣습니다.

pages = notion.search(filter={"property": "object", "value": "page"},
                      page_size=20)

pages['results'] 라는 값을 꺼내보시면 하나의 뭉테기 정보(뭉텅이 강원도 사투리)가 Page 하나의 정보라 보시면 됩니다.

{'title': {'id': 'title',
           'title': [{'annotations': {'bold': False,
                                      'code': False,
                                      'color': 'default',
                                      'italic': False,
                                      'strikethrough': False,
                                      'underline': False},
                      'href': None,
                      'plain_text': '기획서 작업',
                      'text': {'content': '기획서 작업)',
                               'link': None},
                      'type': 'text'}],
           'type': 'title'}}

기본 페이지의 property 이름은 title입니다. property 에 이름이 지정되어 있다면 지정된 이름로도 표기될 수 있습니다. 하기와 같은 경우 생성일property의 이름입니다.

'생성일': {'created_time': '2022-06-07T00:46:00.000Z',
         'id': 'BQmN',
         'type': 'created_time'}

되돌아가서 일반 페이지라고 가정했을때 pages['results']에서 'title'에 해당되는 값의 정보를 타고 들어가다 보면 title 이름을 가져올 수 있습니다.

for 문을 선언해서 pages['results']의 값을 하나씩 꺼내보겠습니다.

for page in pages['results']:
   pprint(page['properties'])

page['properties']를 찍으면 다음과 같이 나옵니다.

{'title': {'id': 'title',
           'title': [{'annotations': {'bold': False,
                                      'code': False,
                                      'color': 'default',
                                      'italic': False,
                                      'strikethrough': False,
                                      'underline': False},
                      'href': None,
                      'plain_text': '기획서 작업',
                      'text': {'content': '기획서 작업)',
                               'link': None},
                      'type': 'text'}],
           'type': 'title'}}

위 정보에서 title 정보를 가저오는 방법은 두가지가 있습니다.

  • page['properties']['title']['title'][0]['plain_text']
  • page['properties']['title']['title'][0]['text']['content']

두가지 중 한가지 방법 택해서 페이지 제목을 가져오시면 됩니다.

page_title_find = '기획서 작업'

for page in pages['results']:
   page_title = page['properties']['title']['title'][0]['plain_text']
   if page_title_find = page_title:
      print('찾는 페이지가 존재합니다.')
      title_id = page['id']

위와 같이 코드를 실행 시키면, title_id 에는 '기획서 작업'이라는 제목을 가진 페이지의 id가 입력되게 됩니다.

profile
QA Specialist

0개의 댓글