import pywikibot

site = pywikibot.Site("en", "wikipedia")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q7295133")
# page = pywikibot.Page(site, "Trump")
# item = pywikibot.ItemPage.fromPage(page)
item.isDisambig()
False
print(item.get().keys())
claims_dict = item.get()['descriptions']
print(claims_dict)
for clm in claims_dict['P31']:
    print(clm.getTarget())
dict_keys(['labels', 'aliases', 'descriptions', 'claims', 'sitelinks'])
{'zh-tw': '維基媒體消歧義頁', 'ckb': 'پەڕەی ڕوونکردنەوەی ویکیمیدیا', 'hi': 'बहुविकल्पी पृष्ठ', 'ms': 'Laman nyahkekaburan', 'yue': '維基媒體搞清楚頁', 'id': 'Halaman disambiguasi', 'sr': 'вишезначна одредница на Викимедији', 'sk': 'rozlišovacia stránka', 'tr': 'Vikimedya anlam ayrımı sayfası', 'zh-mo': '維基媒體消歧義頁', 'fr': "page d'homonymie d'un projet Wikimédia", 'lv': 'Wikimedia projekta nozīmju atdalīšanas lapa', 'vi': 'trang định hướng Wikimedia', 'ca': 'Pàgina de desambiguació', 'hr': 'razdvojbena stranica na Wikimediji', 'he': 'דף פירושונים', 'et': 'Wikimedia täpsustuslehekülg', 'de': 'Wikimedia-Begriffsklärungsseite', 'fi': 'täsmennyssivu', 'nn': 'Wikimedia-fleirtydingsside', 'ja': 'ウィキメディアの曖昧さ回避ページ', 'eo': 'apartigilo', 'cs': 'rozcestník na projektech Wikimedia', 'is': 'aðgreiningarsíða á Wikipediu', 'nl': 'Wikimedia-doorverwijspagina', 'en-gb': 'Wikimedia disambiguation page', 'bs': 'čvor stranica na Wikimediji', 'pt-br': 'página de desambiguação de um projeto da Wikimedia', 'zh-hant': '維基媒體消歧義頁', 'mk': 'појаснителна страница', 'ro': 'pagină de dezambiguizare Wikimedia', 'nds': 'Sied för en mehrdüdig Begreep op Wikimedia', 'zh': '维基媒体消歧义页', 'uk': 'сторінка значень в проекті Вікімедіа', 'ru': 'страница значений в проекте Викимедиа', 'hu': 'egyértelműsítő lap', 'en': 'Wikipedia disambiguation page', 'ka': 'მრავალმნიშვნელოვანი', 'ko': '위키미디어 동음이의어 문서', 'pl': 'strona ujednoznaczniająca w projekcie Wikimedia', 'bg': 'Уикимедия пояснителна страница', 'en-ca': 'Wikimedia disambiguation page', 'zh-hans': '维基媒体消歧义页', 'zh-cn': '维基媒体消歧义页', 'lb': 'Wikimedia-Homonymiesäit', 'sco': 'Wikimedia disambiguation page', 'gl': 'páxina de homónimos de Wikimedia', 'fa': 'یک صفحهٔ ابهام\u200cزدایی در ویکی\u200cپدیا', 'el': 'σελίδα αποσαφήνισης', 'min': 'laman disambiguasi', 'es': 'desambiguación de Wikipedia', 'hy': 'Վիքիմեդիայի նախագծի բազմիմաստության փարատման էջ', 'zh-hk': '維基媒體消歧義頁', 'it': 'pagina di disambiguazione di un progetto Wikimedia', 'pt': 'página de desambiguação de um projeto da Wikimedia', 'gsw': 'Wikimedia-Begriffsklärigssite', 'da': 'Wikimedia-flertydigside', 'bn': 'উইকিমিডিয়া দ্ব্যর্থতা নিরসন পাতা', 'ar': 'صفحة توضيح لويكيميديا', 'zh-my': '维基媒体消歧义页', 'or': 'ବହୁବିକଳ୍ପ ପୃଷ୍ଠା', 'nb': 'Wikimedia-pekerside', 'sv': 'grensida', 'gu': 'સ્પષ્ટતા પાનું', 'zh-sg': '维基媒体消歧义页'}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-76-7d15ba7fecbf> in <module>()
      2 claims_dict = item.get()['descriptions']
      3 print(claims_dict)
----> 4 for clm in claims_dict['P31']:
      5     print(clm.getTarget())

KeyError: 'P31'
page
Page('Trump')
import pywikibot
from pywikibot import textlib

site = pywikibot.Site('wikidata','wikidata')
repo = site.data_repository()

start = 0
end = 37

def count_constraint_templates(tmpl_list):
    """
    Takes a list of templates, sums usage in dictionary, returns

    This function takes a list of templates, iterates over them
    and counts the various constraint templates. Returns the usage
    in a dictionary where the key is the name of the constraint
    template and the value is an integer of the number of usages.
    """
    tmpl_use = {}

    for tmpl in tmpl_list:
        name = tmpl[0]

        if name.startswith("Constraint") == True:
            if name in tmpl_use.keys():
                tmpl_use[name] = tmpl_use[name] + 1
            else:
                tmpl_use[name] = 1

    return tmpl_use

for i in range(start, end + 1):
    # line below will call the talk page of the respective property
    prop_page = pywikibot.Page(site, "P{}".format(i), 121)
    pywikibot.output("--> {}".format(prop_page.title()))

    if prop_page.exists() == False:
        pywikibot.output("Not found.\n")
        continue

    page_str = prop_page.get()
    tmpl_list = textlib.extract_templates_and_params(page_str)
    tmpl_use = count_constraint_templates(tmpl_list)
    print(tmpl_use)
--> Property talk:P0
Not found.

--> Property talk:P1
Not found.

--> Property talk:P2
Not found.

--> Property talk:P3
Not found.

--> Property talk:P4
Not found.

--> Property talk:P5
Not found.

--> Property talk:P6
{'Constraint:Contemporary': 1}
{}
--> Property talk:P7
--> Property talk:P8
Not found.

--> Property talk:P9
{}
--> Property talk:P10
{}
--> Property talk:P11
Not found.

--> Property talk:P12
Not found.

--> Property talk:P13
Not found.

--> Property talk:P14
--> Property talk:P15
{}
{}
--> Property talk:P16
--> Property talk:P17
{}
{}
--> Property talk:P18
{}
--> Property talk:P19
{}
--> Property talk:P20
{}
--> Property talk:P21
{}
{}
--> Property talk:P22
--> Property talk:P23
Not found.

--> Property talk:P24
Not found.

--> Property talk:P25
{'Constraint:Contemporary': 1}
--> Property talk:P26
{'Constraint:Contemporary': 1}
--> Property talk:P27
{'Constraint:Contemporary': 1}
--> Property talk:P28
Not found.

--> Property talk:P29
Not found.

--> Property talk:P30
{}
--> Property talk:P31
{}
--> Property talk:P32
Not found.

--> Property talk:P33
Not found.

--> Property talk:P34
Not found.

--> Property talk:P35
--> Property talk:P36
{}
{}
--> Property talk:P37
{}