5

With the following code it is possible to extract data from entities in Wikidata:

import requests

API_ENDPOINT = "https://www.wikidata.org/w/api.php"

query = "wikipedia"

params = {
    'action': 'wbsearchentities',
    'format': 'json',
    'language': 'en',
    'search': query
}

r = requests.get(API_ENDPOINT, params = params)

print(r.json()['search'][0])

and the output is:

{'repository': '', 'id': 'Q52', 'concepturi': 'http://www.wikidata.org/entity/Q52', 'title': 'Q52', 'pageid': 170, 'url': '//www.wikidata.org/wiki/Q52', 'label': 'Wikipedia', 'description': 'free online encyclopedia that anyone can edit', 'match': {'type': 'label', 'language': 'en', 'text': 'Wikipedia'}}

But going to the concepturi 'http://www.wikidata.org/entity/Q52 I see more information than reported here in the json file, specifically I am interested on motto text field.

How could I get more information from Wikidata? (this is an example more could be shown where the query outputs less info than contained in Wikidata).

5
  • This question is not programming related, it is about the Wikidata API. I suggest you read the documentation. Commented Jul 19, 2018 at 10:09
  • 1
    Use the wbgetentities URL: wikidata.org/w/… Commented Jul 19, 2018 at 10:13
  • Thanks @intentionally left blank, Im a little bit lost, I am reading the api documentation, hoping see something clear enough. Commented Jul 19, 2018 at 10:13
  • In SPARQL: paws-public.wmflabs.org/paws-public/User:Luitzen/Motto.ipynb Commented Jul 25, 2018 at 10:13
  • Please, see Getting readable results from Wikidata. Commented Jul 25, 2018 at 21:21

1 Answer 1

3

you may use wikidata python module qwikidata

from qwikidata.sparql  import return_sparql_query_results

query_string = """
        SELECT $WDid
         WHERE {
          ?WDid (wdt:P279)* wd:Q4022
        }"""

res = return_sparql_query_results(query_string)

for row in res["results"]["bindings"]:
   print(row["yourFieldName"]["value"])
Sign up to request clarification or add additional context in comments.

1 Comment

There seems to be an even more mature python package called wikidata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.