pythonsparqlwikidata

Unable to get label querying Wikidata SPARQL endpoint


I'm trying to query the Wikidata SPARQL endpoint through a Python script. I'm successfully getting the QID of the entities but I'm unable to get the label.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?item ?itemLabel WHERE {
      {
        ?item p:P31 ?statement0.
        ?statement0 (ps:P31/(wdt:P279*)) wd:Q103890060.
        FILTER(NOT EXISTS { ?statement0 prov:wasDerivedFrom ?reference. })
        SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".
      }
    }
  }

In the query service app, this is the result I'm getting: enter image description here

However, when I query using a script, I don't get the label, I only get the uri and the qid:

{
   "item":{
      "type":"uri",
      "value":"http://www.wikidata.org/entity/Q5449566"
   },
   "itemLabel":{
      "type":"literal",
      "value":"Q5449566"
   }
}

I need the URI and the label, not the QID.

I'm unsure what's wrong with my query. I used the python code provided by the Wikidata Query Service to see if I was missing a parameter or something, but that also provided the above result.


Solution

  • According to the comments and to the Wikidata documentation:

    The Wikidata Query Service website automatically replaces [AUTO_LANGUAGE] with the language code of current user's interface. For example, if the user's UI is in French, the SPARQL's code bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" will be converted to bd:serviceParam wikibase:language "fr,en" before being sent to the query service.

    When you are building your request from a script don't use [AUTO_LANGUAGE], use a label such as "en" or "fr".