Wrapper around the Wikidata SPARQL endpoint that respects the Wikidata usage policy.
Project description
title: README author: Jan-Michael Rye
Synopsis
A light wrapper around SPARQLWrapper for Wikidata's SPARQL query service. The wrapper attempts to follow Wikidata's usage policy regarding user-agent strings and query frequency.
The wrapper also wraps expected exceptions in a custom exception class to facilitate error handling.
Links
GitLab
Other Repositories
Usage
from wikidata_sparqlwrapper import (
WikidataSPARQLWrapper,
WikidataSPARQLWrapperError,
construct_user_agent_string,
)
# Construct a user-agent string that uniquely identifies your code and which
# includes a contact email address.
agent = construct_user_agent_string(name="MyApp", email="my_email@example.com")
# Instantiate the Wikidata SPARQL wrapper.
wrapper = WikidataSPARQLWrapper(user_agent=agent)
# Construct a SPARQL query.
query = """\
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""
# Get the SPARQLWrapper query object directly. The format will be set to JSON.
# This example includes error handling but it is not necessary here. The user
# may catch the error elsewhere such as in the main function.
try:
response = wrapper.query(query)
except WikidataSPARQLWrapperError as err:
# Handle the error here.
# Get the bindings directly. The remarks about error handling from above apply
# here too.
try:
bindings = wrapper.query_bindings(query)
except WikidataSPARQLWrapperError as err:
# Handle the error here.
# Do something with the bindings.
for binding in bindings:
print(binding["itemLabel"]["value"])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wikidata_sparqlwrapper-2025.1.tar.gz
.
File metadata
- Download URL: wikidata_sparqlwrapper-2025.1.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
d95f4c65efb511de88dafa74c91bf578eb8803735fbb08fda85c9a6fbc4471d7
|
|
MD5 |
6d5560e90e6a73808afecf374f417615
|
|
BLAKE2b-256 |
aae117d638c7780054748d0e87ce03e38af1b0a0f10c6c838f2bfebec6d3b1e8
|
File details
Details for the file wikidata_sparqlwrapper-2025.1-py3-none-any.whl
.
File metadata
- Download URL: wikidata_sparqlwrapper-2025.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
2619f5a51ba48bd57de9184d925807db073ee0af0a5aee9722205a0f60f950eb
|
|
MD5 |
880977a3e38a7757743a49732b412b50
|
|
BLAKE2b-256 |
b75bb3b0b38cb26a3fdbc56dc3448966848fc6cfc6eb42e20e867589398f94bb
|