Search code examples
sparqlsemantic-webwikidata

Can't get film titles from wikidata


I am trying to get movie titles from wikidata with a query.

I already tried this:

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>

SELECT ?q ?film_title ?actor ?genre WHERE {
  ?q wdt:P31 wd:Q11424.
}

LIMIT 10

The output of this are like this: wd:Q372

however we are trying to get the movie_title of this number and not this number.


Solution

  • Each movie instance (like wd:Q372) is of a type that has some literal properties like label (or movie title in this case) and some relationships with other entities such as an actor. In order to get the literal values (like title, etc.) you need to query for those. For example, here is the query that gets all the movie titles, their genres and their actors (I assumed that you want only the english labels And I have limited the results to 10).

    PREFIX wd: <http://www.wikidata.org/entity/>
    PREFIX wdt: <http://www.wikidata.org/prop/direct/>
    PREFIX wikibase: <http://wikiba.se/ontology#>
    PREFIX p: <http://www.wikidata.org/prop/>
    PREFIX v: <http://www.wikidata.org/prop/statement/>
    PREFIX q: <http://www.wikidata.org/prop/qualifier/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    
    SELECT ?q ?film_title ?actor ?genre WHERE {
      ?q wdt:P31 wd:Q11424.
      ?q rdfs:label ?film_title filter (lang(?film_title) = "en").
      ?q wdt:P136 ?genreID.
      ?genreID rdfs:label ?genre filter (lang(?genre) = "en").
      ?q wdt:P161 ?actorID.
      ?actorID rdfs:label ?actor filter (lang(?actor) = "en").
    }limit 10