Search code examples
pythonsparqlwdqs

SPARQL query to get several informations from a given wiki-ID


I have the information from several authors in a table. In this table, a column contains the wiki-id for each author. I want to use the wiki-ids to get further information, such as gender, place of birth, place of death, etc. My plan is to use a Python script to loop over the table and run for each author a SPARQL query, get their data and add it to the table.

The problem is that I don't know how to query "get information from author that has wiki-id QXXXX". For example, let us take the author Almudena Grandes (Wiki-id Q274348, https://www.wikidata.org/wiki/Q274348). How do I make a SPARQL query that get her gender, place and date of birth?

I have been writing other queries about Spanish authors, like this: https://w.wiki/468x


Solution

  • Add

    bind(wd:Q274348 as ?person)
    

    to your query about Spanish authors. With this change, you will not get the results of all Spanish authors but only of Almudena Grandes.

    A bit simplified, the query looks then like

    SELECT distinct ?person ?personLabel ?nationalityLabel ?databirth ?placebirthLabel ?genderLabel
    WHERE
    {
      bind(wd:Q274348 as ?person)
      ?person wdt:P27 ?nationality.
      optional {?person wdt:P569 ?databirth}.
      optional {?person wdt:P19 ?placebirth}.
      optional {?person wdt:P21 ?gender}.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }