Skip to main content

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"])

Release files for wikidata-sparqlwrapper 2025.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wikidata-sparqlwrapper 2025.1
File Size Uploaded
wikidata_sparqlwrapper-2025.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wikidata-sparqlwrapper 2025.1
File Interpreter ABI Platform
wikidata_sparqlwrapper-2025.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / wikidata_sparqlwrapper-2025.1.tar.gz

Download URL wikidata_sparqlwrapper-2025.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d95f4c65efb511de88dafa74c91bf578eb8803735fbb08fda85c9a6fbc4471d7
BLAKE2b-256 checksum
How to use checksums
aae117d638c7780054748d0e87ce03e38af1b0a0f10c6c838f2bfebec6d3b1e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.1

Release files / wikidata_sparqlwrapper-2025.1-py3-none-any.whl

Download URL wikidata_sparqlwrapper-2025.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2619f5a51ba48bd57de9184d925807db073ee0af0a5aee9722205a0f60f950eb
BLAKE2b-256 checksum
How to use checksums
b75bb3b0b38cb26a3fdbc56dc3448966848fc6cfc6eb42e20e867589398f94bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.1

Release history Release notifications | RSS feed

This release

2025.1 This release

2 release files

2024.2

2 release files

2024.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page