Pywikibot tutorial

According to the Wikidata wiki pywikibot tutorial

This part finds the item via en.wp

#!/usr/bin/python3
import pywikibot

site = pywikibot.Site("en", "wikipedia")
page = pywikibot.Page(site, "Douglas Adams")
item = pywikibot.ItemPage.fromPage(page)

print(item)
[[wikidata:Q42]]
print(dir(item))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_cache_attrs', '_cmpkey', '_content', '_cosmetic_changes_hook', '_defined_by', '_diff_to', '_getInternals', '_get_parsed_page', '_isredir', '_latest_cached_revision', '_link', '_namespace', '_namespace_obj', '_normalizeData', '_normalizeLanguages', '_pageid', '_revid', '_revisions', '_save', 'addClaim', 'aliases', 'applicable_protections', 'aslink', 'autoFormat', 'backlinks', 'botMayEdit', 'canBeEdited', 'categories', 'change_category', 'claims', 'clear_cache', 'concept_uri', 'concept_url', 'content_model', 'contributingUsers', 'contributors', 'coordinates', 'data_item', 'data_repository', 'defaultsort', 'delete', 'depth', 'descriptions', 'editAliases', 'editDescriptions', 'editEntity', 'editLabels', 'editTime', 'embeddedin', 'encoding', 'entity_type', 'exists', 'expand_text', 'extlinks', 'fromPage', 'from_entity_uri', 'fullVersionHistory', 'full_url', 'get', 'getCategoryRedirectTarget', 'getCreator', 'getDeletedRevision', 'getID', 'getLatestEditors', 'getMovedTarget', 'getOldVersion', 'getRedirectTarget', 'getReferences', 'getRestrictions', 'getSitelink', 'getTemplates', 'getVersionHistory', 'getVersionHistoryTable', 'getdbName', 'id', 'image_repository', 'imagelinks', 'interwiki', 'isAutoTitle', 'isCategory', 'isCategoryRedirect', 'isDisambig', 'isEmpty', 'isFlowPage', 'isImage', 'isIpEdit', 'isRedirectPage', 'isStaticRedirect', 'isTalkPage', 'is_categorypage', 'is_filepage', 'is_flow_page', 'is_valid_id', 'iterlanglinks', 'iterlinks', 'itertemplates', 'labels', 'langlinks', 'lastNonBotUser', 'latestRevision', 'latest_revision', 'latest_revision_id', 'linkedPages', 'loadDeletedRevisions', 'markDeletedRevision', 'mergeInto', 'merge_history', 'move', 'moved_target', 'namespace', 'oldest_revision', 'pageAPInfo', 'page_image', 'pageid', 'permalink', 'preloadText', 'previousRevision', 'previous_revision_id', 'properties', 'protect', 'protection', 'purge', 'put', 'put_async', 'removeClaims', 'removeImage', 'removeSitelink', 'removeSitelinks', 'replaceImage', 'repo', 'revision_count', 'revisions', 'save', 'section', 'sectionFreeTitle', 'setSitelink', 'setSitelinks', 'set_redirect_target', 'site', 'sitelinks', 'templates', 'text', 'title', 'titleForFilename', 'titleWithoutNamespace', 'title_pattern', 'toJSON', 'toggleTalkPage', 'touch', 'undelete', 'urlname', 'userName', 'version', 'watch']
item_dict = item.get()
print(item_dict.keys())
dict_keys(['aliases', 'labels', 'descriptions', 'claims', 'sitelinks'])
print(item_dict['descriptions'].keys())
dict_keys(['en', 'fr', 'en-gb', 'nb', 'it', 'de', 'es', 'ru', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-sg', 'zh-my', 'zh', 'zh-hk', 'zh-tw', 'zh-mo', 'ca', 'fi', 'cs', 'sv', 'pt-br', 'ta', 'sl', 'da', 'nl', 'pt', 'pl', 'lv', 'sr', 'sr-ec', 'sr-el', 'eo', 'bar', 'br', 'ja', 'nn', 'tr', 'si', 'vi', 'cy', 'gu', 'uk', 'ro', 'hu', 'fa', 'af', 'mk', 'el', 'hy', 'bg', 'ne', 'he', 'de-at', 'de-ch', 'gsw', 'nds', 'kn', 'pa', 'ar', 'tl', 'eu', 'hr', 'ko', 'sw', 'th', 'en-ca', 'gd', 'ka'])

This part goes straight to Wikidata using an item number.

site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q42")
print(item.get()['descriptions'].keys())
dict_keys(['en', 'fr', 'en-gb', 'nb', 'it', 'de', 'es', 'ru', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-sg', 'zh-my', 'zh', 'zh-hk', 'zh-tw', 'zh-mo', 'ca', 'fi', 'cs', 'sv', 'pt-br', 'ta', 'sl', 'da', 'nl', 'pt', 'pl', 'lv', 'sr', 'sr-ec', 'sr-el', 'eo', 'bar', 'br', 'ja', 'nn', 'tr', 'si', 'vi', 'cy', 'gu', 'uk', 'ro', 'hu', 'fa', 'af', 'mk', 'el', 'hy', 'bg', 'ne', 'he', 'de-at', 'de-ch', 'gsw', 'nds', 'kn', 'pa', 'ar', 'tl', 'eu', 'hr', 'ko', 'sw', 'th', 'en-ca', 'gd', 'ka'])

Getting claims

#Get the item
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q2225")

item_dict = item.get() #Get the item dictionary
clm_dict = item_dict["claims"] # Get the claim dictionary
print(clm_dict)
{'P1360': [<pywikibot.page.Claim object at 0x7f18b28a8dd8>], 'P279': [<pywikibot.page.Claim object at 0x7f18b2863320>], 'P1036': [<pywikibot.page.Claim object at 0x7f18b28634e0>], 'P373': [<pywikibot.page.Claim object at 0x7f18b28635c0>], 'P227': [<pywikibot.page.Claim object at 0x7f18b2863630>], 'P517': [<pywikibot.page.Claim object at 0x7f18b28636a0>, <pywikibot.page.Claim object at 0x7f18b28637f0>, <pywikibot.page.Claim object at 0x7f18b28639e8>], 'P508': [<pywikibot.page.Claim object at 0x7f18b2863b38>], 'P910': [<pywikibot.page.Claim object at 0x7f18b2863c50>], 'P349': [<pywikibot.page.Claim object at 0x7f18b2863e10>], 'P1122': [<pywikibot.page.Claim object at 0x7f18b2863ef0>], 'P1123': [<pywikibot.page.Claim object at 0x7f18b2863f60>], 'P646': [<pywikibot.page.Claim object at 0x7f18b286b080>], 'P1014': [<pywikibot.page.Claim object at 0x7f18b286b0f0>], 'P1051': [<pywikibot.page.Claim object at 0x7f18b286b160>], 'P61': [<pywikibot.page.Claim object at 0x7f18b286b320>], 'P575': [<pywikibot.page.Claim object at 0x7f18b286b3c8>], 'P31': [<pywikibot.page.Claim object at 0x7f18b286b518>], 'P2069': [<pywikibot.page.Claim object at 0x7f18b286b630>], 'P2152': [<pywikibot.page.Claim object at 0x7f18b286b6d8>], 'P2200': [<pywikibot.page.Claim object at 0x7f18b286b780>], 'P2375': [<pywikibot.page.Claim object at 0x7f18b286b8d0>], 'P18': [<pywikibot.page.Claim object at 0x7f18b286b9b0>], 'P3417': [<pywikibot.page.Claim object at 0x7f18b286bc18>], 'P3827': [<pywikibot.page.Claim object at 0x7f18b286bc50>], 'P3916': [<pywikibot.page.Claim object at 0x7f18b286bcc0>], 'P2067': [<pywikibot.page.Claim object at 0x7f18b286be10>], 'P138': [<pywikibot.page.Claim object at 0x7f18b286bf28>], 'P683': [<pywikibot.page.Claim object at 0x7f18b286f080>]}
#This is not in the tutorial but surprisingly useful
for clm in clm_dict:
    print(clm)
P1360
P279
P1036
P373
P227
P517
P508
P910
P349
P1122
P1123
P646
P1014
P1051
P61
P575
P31
P2069
P2152
P2200
P2375
P18
P3417
P3827
P3916
P2067
P138
P683
clm_list = clm_dict["P2069"]
for clm in clm_list:
    print(clm)
    print(dir(clm))
<pywikibot.page.Claim object at 0x7f18b286b630>
['SNAK_TYPES', 'TARGET_CONVERTER', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_formatDataValue', '_formatValue', '_type', 'addQualifier', 'addSource', 'addSources', 'changeRank', 'changeSnakType', 'changeTarget', 'fromJSON', 'getID', 'getRank', 'getSnakType', 'getSources', 'getTarget', 'getType', 'has_qualifier', 'hash', 'id', 'isQualifier', 'isReference', 'on_item', 'qualifierFromJSON', 'qualifiers', 'rank', 'referenceFromJSON', 'removeQualifier', 'removeQualifiers', 'removeSource', 'removeSources', 'repo', 'setRank', 'setSnakType', 'setTarget', 'snak', 'snaktype', 'sources', 'target', 'target_equals', 'toJSON', 'type', 'types', 'value_types']
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q2225")

item_dict = item.get()
clm_dict = item_dict["claims"]
clm_list = clm_dict["P2069"]

for clm in clm_list:
    print(clm.toJSON())
{'mainsnak': {'snaktype': 'value', 'property': 'P2069', 'datatype': 'quantity', 'datavalue': {'value': {'amount': '-1.00115965218076', 'upperBound': '-1.00115965218075', 'lowerBound': '-1.00115965218077', 'unit': 'http://www.wikidata.org/entity/Q737120'}, 'type': 'quantity'}}, 'type': 'statement', 'id': 'Q2225$edaaaf4e-48fd-6503-016c-27d857e55f40', 'rank': 'normal'}
for clm in clm_list:
    clm_trgt = clm.getTarget()
    print(clm_trgt.upperBound) # this is a property, not an index
-1.00115965218075
for clm in clm_list:
    clm_trgt = clm.getTarget()
    print(clm_trgt) #not in tutorial
{
    "amount": "-1.00115965218076",
    "lowerBound": "-1.00115965218077",
    "unit": "http://www.wikidata.org/entity/Q737120",
    "upperBound": "-1.00115965218075"
}
print(type(clm_trgt)) #one claim target content
print(type(clm)) #one claim object
print(type(clm_list)) #all claims of a particular property in an item
print(type(clm_dict)) #all claims of an item
print(type(item_dict))
print(type(item))
<class 'pywikibot.WbQuantity'>
<class 'pywikibot.page.Claim'>
<class 'list'>
<class 'dict'>
<class 'dict'>
<class 'pywikibot.page.ItemPage'>

Basic Python data structure info: this Wikiversity page

This is the end of tutorial part 1.