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).

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
PeCaDe
  • 277
  • 1
  • 8
  • 33
  • This question is not programming related, it is about the Wikidata API. I suggest you read [the documentation](https://www.wikidata.org/wiki/Wikidata:Data_access). –  Jul 19 '18 at 10:09
  • 1
    Use the `wbgetentities` URL: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q52&format=json –  Jul 19 '18 at 10:13
  • Thanks @intentionally left blank, Im a little bit lost, I am reading the api documentation, hoping see something clear enough. – PeCaDe Jul 19 '18 at 10:13
  • In SPARQL: http://paws-public.wmflabs.org/paws-public/User:Luitzen/Motto.ipynb – Stanislav Kralin Jul 25 '18 at 10:13
  • Please, see [Getting readable results from Wikidata](https://stackoverflow.com/questions/31266398/getting-readable-results-from-wikidata/31290824). – Orienteerix Jul 25 '18 at 21:21

1 Answers1

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"])
Aftab Alam
  • 64
  • 1
  • 4