cURL examples to query Wikidata

The SPARQL endpoint is http://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql and it has a Web form to fire queries. However http://www.wikidata.org/prop/direct/P31 (“instance of”) tells you what the entity is.

The repository doesn’t have named graphs, or at least the SPARQL endpoint rejects graph queries. The classes of entities (rdf:type) are not described in the repository.

To find the HTML page of an entity (such as https://www.wikidata.org/entity/Q866405), simply replace /entity/ with /wiki/.

By default the SPARQL endpoint returns the results in SPARQL Results XML format but

  • adding -H "Accept: application/json" to the cURL command gets them in JSON Query Results
  • adding -H "Accept: text/csv" to the cURL command gets them in CSV format (the most readable).

Don’t forget to URL encode the query 😉

The list of entity types

Description of an entity (East Antarctica)

describe queries return all the triples in which the selected entity is subject (position 1/3). Certain SPARQL endpoints (such as this one) also return the triples in which the entity is object (position 3/3). The query result being a graph and not a table like for SPARQL Results, the default format is RDF/XML. JSON and Turtle (text/turtle) can also be requested.

4 thoughts on “cURL examples to query Wikidata”

    1. La commande

      curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql –data-urlencode query=’
      select distinct ?type where {
      ?thing ?type
      }
      limit 20′ -H “Accept: text/csv”

      fonctionne très bien. Maintenant, quand la requête est complexe, j’aimerais pouvoir stocker la requête SPARQL dans un fichier sparql.txt et requête avec une commande du type :

      curl -G https://query.wikidata.org/bigdata/namespace/wdq/sparql –data-urlencode query=sparql.txt -H “Accept: text/csv”

      Mais je n’arrive pas à comprendre comment écrire cette requête.

Comments are closed.