4

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

4

1 回答 1

2

你可以使用 wikidata python 模块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"])
于 2019-10-25T17:31:23.373 回答