Skip to main content

Wrapper around that 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.
response = wrapper.query(query)

# Get the bindings directly.
bindings = wrapper.query_bindings(query)

# Do something with the bindings.
for binding in bindings:
    print(binding["itemLabel"]["value"])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wikidata_sparqlwrapper-2024.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

wikidata_sparqlwrapper-2024.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file wikidata_sparqlwrapper-2024.2.tar.gz.

File metadata

File hashes

Hashes for wikidata_sparqlwrapper-2024.2.tar.gz
Algorithm Hash digest
SHA256 1cf09ac99ad0524a8778a24f16d9761f3c7a3cb9503735337ee2e2b7131fbc07
MD5 bef847374c07ee60f2dad5357c24f1b5
BLAKE2b-256 bae1179c55e78f5bd7513b1242f6ac260aaefc185645d84936339e7e77b80c89

See more details on using hashes here.

File details

Details for the file wikidata_sparqlwrapper-2024.2-py3-none-any.whl.

File metadata

File hashes

Hashes for wikidata_sparqlwrapper-2024.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2fb89b0abad79a386d4ac0a8c12956751f08645a6d4617ffd1fac7e95a0a9b76
MD5 84f67cea49d0236546e1bad556fcd9e9
BLAKE2b-256 b884cbd034163e96c4586beb51f5c43591b9410da36fff88b63dd5cb520c6404

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page