Created
December 11, 2023 18:25
-
-
Save salgo60/e7e3fbd758d097ff0772ac3176449211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install sparqlwrapper | |
# https://rdflib.github.io/sparqlwrapper/ | |
import sys | |
from SPARQLWrapper import SPARQLWrapper, JSON | |
endpoint_url = "https://query.wikidata.org/sparql" | |
query = """#title: Same as SPA property P4819 | |
SELECT ?person ?personLabel ?SPA ?birth ?death ?birthLocationLabel ?deathLocationLabel | |
{ | |
{ | |
SELECT ?person (COUNT(?SPA) AS ?cnt) (sample(?SPA) as ?SPA) { | |
?person wdt:P4819 ?SPA | |
} | |
GROUP BY ?person ORDER BY DESC(?cnt) | |
} | |
OPTIONAL {?person wdt:P569 ?birth} | |
OPTIONAL {?person wdt:P570 ?death} | |
OPTIONAL {?person wdt:P19 ?birthLocation} | |
OPTIONAL {?person wdt:P20 ?deathLocation} | |
SERVICE wikibase:label { bd:serviceParam wikibase:language "sv,en". } | |
} | |
ORDER BY DESC(?cnt) | |
""" | |
def get_results(endpoint_url, query): | |
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1]) | |
# TODO adjust user agent; see https://w.wiki/CX6 | |
sparql = SPARQLWrapper(endpoint_url, agent=user_agent) | |
sparql.setQuery(query) | |
sparql.setReturnFormat(JSON) | |
return sparql.query().convert() | |
results = get_results(endpoint_url, query) | |
for result in results["results"]["bindings"]: | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment