qwikidata.sparql module

Module for the Wikidata SPARQL endpint.

get_subclasses_of_item(item_id, return_qids=True)[source]

Return all subclasses of a wikidata item.

Finds all items where a chain of the following form exists,:

Qid_i -[P279]-> Qid_j ... Qid_k -[P279]-> item_id

Will always include the item itself in the return results. Note that property P279 = “subclass of”.

Parameters
  • item_id (str) – The item to use as the end of the chain.

  • return_qids (bool, optional) – If false, the SPARQL query result is returned unaltered. If true, a list of item id string is returned instead. See examples.

Examples

We can get all item IDs that are subclasses of Q6256,

>>> get_subclasses_of_item('Q6256')
['Q6256',
 'Q112099',
 'Q123480',
 ...
 'Q4994005',
 'Q6805624',
 'Q15895923']
>>> get_subclasses_of_item('Q6256', return_qids=False)
{'head': {'vars': ['WDid']},
 'results': {'bindings': [
   {'WDid': {'type': 'uri',
     'value': 'http://www.wikidata.org/entity/Q6256'}},
   {'WDid': {'type': 'uri',
     'value': 'http://www.wikidata.org/entity/Q112099'}},
   ...
   {'WDid': {'type': 'uri',
     'value': 'http://www.wikidata.org/entity/Q6805624'}},
   {'WDid': {'type': 'uri',
     'value': 'http://www.wikidata.org/entity/Q15895923'}}]}}
Return type

Union[List[str], Dict]

return_sparql_query_results(query_string, wikidata_sparql_url='https://query.wikidata.org/sparql')[source]

Send a SPARQL query and return the JSON formatted result.

Parameters
  • query_string (str) – SPARQL query string

  • wikidata_sparql_url (str, optional) – wikidata SPARQL endpoint to use

Return type

Dict