Search code examples
pythonwikidata

How to get list of entities in python wikidata


I need to get all the info about some writers in wikidata

For example - https://www.wikidata.org/wiki/Q39829

My code

from wikidata.client import Client
client = Client()
entity = client.get('Q39829', load=True)

# Spouse
spouse_prop = client.get('P26')
spouse = entity[spouse_prop]
print('Mother: ', spouse.label)

# Children
child_prop = client.get('P40')
child = entity[child_prop]
print('Children: ', dir(child))
print(child.label)

Problem

The problem is with the properties with the list of values

For example, he has 3 children And in the result I have only one name

Question

How can I get the list of all children?


Solution

  • From the docs for Wikidata:

    Although it implements Mapping[EntityId, object], it [entity] actually is multidict. See also getlist() method.

    Which means you can do:

    >>> [c.label for c in entity.getlist(child_prop)]
    [m'Joe Hill', m'Owen King', m'Naomi King']