Wikidata

From XennisWiki
Jump to: navigation, search

Wikidata is a collaboratively edited multilingual knowledge base hosted by the Wikimedia Foundation. It is a common source of open data that Wikimedia projects such as Wikipedia can use, and anyone else, under a public domain license. The used data model is the Resource Description Framework. Wikidata is powered by the software Wikibase. (Wikipedia)

API

Query data

The online query service can be used to run queries.

URL

The generic query URL

https://query.wikidata.org/sparql?format=<format>&query=<query>

Example

https://query.wikidata.org/sparql?format=json&query=SELECT%20%3Fitem%20WHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP31%20%3Fp%3B%0A%20%20%20%20wdt%3AP17%20wd%3AQ183.%0A%20%20FILTER(%3Fp%20IN(wd%3AQ22698%2C%20wd%3AQ167346))%0A%7D

Python

Install the pip package sparqlwrapper

import sys
from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """\
SELECT ?item WHERE {
  ?item wdt:P31 ?p .
       ?item wdt:P17 wd:Q183 .
  FILTER (?p IN (wd:Q22698,  wd:Q167346) )
}"""


def get_results(endpoint_url, query):
    user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
    # TODO adjust user agent; see https://w.wiki/CX6
    sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()


results = get_results(endpoint_url, query)

for result in results["results"]["bindings"]:
    print(result)

Request entity

The generic entity URL

https://www.wikidata.org/wiki/Special:EntityData/<id>.<format>

Example

https://www.wikidata.org/wiki/Special:EntityData/Q25089266.json

See also

External links